MVC routing under a domain - asp.net-mvc-4

I have just published an MVC application. It has a Home controller with a method Create.
The URL is http://www.myurl.com
The name of the virtual directory in IIS is myurl.
When I submit the form that posts to Home/Create I'm getting an error because it's trying to send the data to:
http://www.myurl.com/myurl/Home/Create
rather than:
http://www.myurl.com/Home/Create
I tried adding this route:
routes.MapRoute(
"test",
"Home/CreateNewPlayer",
new { controller = "Home", action = "CreateNewPlayer" }
);
but that hasn't made any difference. Can someone please help me out?

You should not host your application in a virtual directory but rather directly inside the website which has a binding to the www.myurl.com domain.

Related

Unable to change swagger ui path

I'm using Swagger / Swashbuckle version 5.6 to generate documentation for my ASP.Net Web API 2 project.
By default API documentation is accessible at URL http://localhost:56081/swagger/ui/index
But, I want it should be available at http://localhost:56081/apihelp/
I searched a lot, tried changing settings in the SwaggerConfig.cs file but nothing seems to make this work.
So, is this even possible? if yes, can anyone help me with this ?
You can add the path to the call of EnableSwaggereUi,
e.g.:
SwaggerConfig.Register(HttpConfiguration config)
{
config.EnableSwaggerUi("apihelp/{*assetPath}", c =>
... // following lines omitted
}
You can then call the UI with the URL http://localhost:56081/apihelp/index
See https://github.com/domaindrivendev/Swashbuckle#custom-routes for reference.
Please note: the default setting 'swagger' redirects automatically to 'swagger/ui/index', but this custom setting does not automaically redirect to 'apihelp/index' when you just use 'apihelp'.
To achieve an automatic redirect you can add the route in WebApiConfig:
config.Routes.MapHttpRoute(
name: "Swagger UI",
routeTemplate: "apihelp",
defaults: null,
constraints: null,
handler: new RedirectHandler(message => message.RequestUri.ToString().TrimEnd('/'), "/index"));
The redirect code is based on v.karbovnichy's answer in How to redirect from root url to /swagger/ui/index?

How to create custom Routing in ASP.NET Core

I need help with URL rewriting. I am new to ASP.NET Core MVC. When I type anything in {param} part then routing should redirect it to my controller.
So if anyone to types in {param} like
https://mydoamin.com/{param}
then it should be redirected to this url:
https://mydoamin.com/{controller}/{action}/{actionurl}={param}
I found the answer for my question. Just define new custom route in your startup.cs file before your default route.
routes.MapRoute(
"Member", // Route name
"{actionURL}", // URL with parameters
new { controller = "Pages", action = "Details" } // Parameter defaults
);
It's working form me.
We can do the same using this approach.
I find this more convenient.
[Http("add/user/{user}/{password}")]
public IActionResult AddUser(string user, string password)
{
//do your things here...
}
I could recommend you to see this blog post from Stephen Walther: ASP.NET 5 Deep Dive: Routing
I am not sure if this works as you want. https://mydoamin.com/{controller}/{action}/{actionurl}={param} seems not to be a valid URL to me. The part {actionurl}={param} is probably the query part which is comes as a key/value pair and starts always with a ?. You could probably fix your routing if your desired URL would look like https://mydoamin.com/{controller}/{action}/?key1=value1&key2=value2
I don't know still this is useful OR not but in asp.net core 3 this worked for me like a charm.
routes.MapRoute(
"MoviesByReleaseDate",
"movies/released/{year}/{month}",
new { controller = "Movies", action = "ByReleaseDate" }
);

Asp.Net Web API action string route mapping

I have a contoller named customer and an action method for GetAllCustomers which recieves single string input parameter as FirstName. The issue is that I want the url to be like
MyAPI/Customer/MyFirstName
but the above stated URL is not working for me instead it working as below
MyAPI/Customer?firstname=MyFirstName
How can I make it work like the first url using ApplicationRouting (nuget package)? I have configured the attrbute route for the action method as below.
[GET("Customer/{firstname}")]
public List<Customer> GetAllCustomersFirstname(String firstname)
{
//code goes here
}
Edit
Here is the reference whatI am trying to achieve but it is not happening for me in ApiController
Attribute Routing Github
Open up ApiConfig in the AppStart folder. You need to add the route for that particular action. I think you'll need more than just what you showed there for route since you have to tell it which action to call and what methods to allow. E.g.
config.Routes.MapHttpRoute(
name: "yourRoute",
routeTemplate: "api/{controller}/{firstname}",
defaults: new { controller = "Customer", action = "GetAllCustomersFirstname" });

MVC4 - My Custom Route isn't working

So I have a custom route in my MVC4 web app below:
routes.MapRoute(
name: "SecondarySportsCategoryLanding",
url: "sports/{name}/{id}",
defaults: new { controller = "FrontCategoriesController", action = "Sports", name = UrlParameter.Optional, id = UrlParameter.Optional }
);
I have that placed above my default one as well. What's happening is when I go to /sports I get this error:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /sports
I have the controller specified, and inside that I have an Action titled Sports with 2 optional parameters (defaulted to nulls). Any ideas?
controller = "FrontCategoriesController" should be controller = "FrontCategories"
Because the framework automatically inserts Controller into the value for you.

Convert ASP.Net MVC4 site to Azure

I've created a ASP.Net MVC 4 web site that has several custom routes which are registered in the ~/App_Start/RouteConfig file.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Empty",
url: "",
defaults: new { controller = "Home", action = "Index", id = 0 }
);
routes.MapRoute(
name: "Session",
url: "{id}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
This site is working great outside of Azure and even works in the Azure emulator, but when I move it up to Azure, it's acting like there are no routes. For instance, when I try to navigate to the root of the web site I get an error stating:
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
This made me think that the route simply wasn't working so I tried going to a another page like http://myapp.cloudapp.net/1234 (which should send 1234 into the home controller's index action. But that action gives me a 404 error.
I've checked my configuration against and new ASP.Net MVC 4 Azure project and they appear to be the same. I've also read through this which gave some pointers but ultimately didn't help.
Any advice on how to troubleshoot this?
Update
This is a WebRole project and is using .Net 4.0 (I already down converted from 4.5).
Have you tried setting a default page as per this article http://www.sunilhaste.com/2011/04/403-forbidden-access-is-denied-error.html
After you have uploaded your Web Role to Azure, you try to access the application and suddenly you get "403-Forbidden, Access denied"
error.
You are able access a page by providing it's complete url ex :- http://xyz.cloudapp.net/Pages/Home.aspx.
You don't have a default.aspx page present at the root level of your website. Solution :- The first thing that you should do is, to
check whether you have added below tag to the web.config file inside
system.webserver tag or not.
<add value="Pages/Home.aspx"/>
I also had had a similar issue and mine was fixed by setting the role to be 2008 R2. Someone in comments to the linked article says
change OS CONFIGURATION from MS SERVER 2008 SP2 to MS SERVER 2008 R2
Also see the following Asp.net and Azure - Can't no longer show my web application suggests checking you have <modules runAllManagedModulesForAllRequests="true" />
Also I assume you are making sure that MVC4 files are being deployed to Azure. Have your RDP'd to the machine and checked event log etc?