I have an ASP.NET Core 3.1 MVC router question.
When I change index route to [Route("Index")] in HomeController enter image description here, and then enter the url localhost:5000/index in web browser, it works!
But in my ASP.NET Core MVC project, the default startup url is localhost:5000/home/index enter image description here.
Question
How can I change the default startup url to localhost:5000/index?
I wish use localhost:5000/home/login to visit the login function in the HomeController.
Add following route before your default route.
endpoints.MapControllerRoute(
name: "customindex",
pattern: "index",
defaults: new { controller="Home", action="Index"});
});
pattern can support default value but it works with common scenario like controller/action but if you want specific pattern then you have to specify pattern and also set defaults so those will supply as route value in order to identify action to call.
Related
I have created a web application using ASP.NET Core 2.1 MVC.
And currently i am having an issue with routing when the application is published.
The url format where the application will be published is this : https://hostername.com/{some_parameter}
All the generated url-s from my application should be "attached" to the above mentioned url.
So i need to have a routing like this :
https://hostername.com/{some_parameter}/{controller}/{action}/{id}
Some examples :
- https://hostername.com/ApplicationName/Home/Profile
- https://hostername.com/ApplicationName/Home/Settings
- https://hostername.com/ApplicationName/FAQ etc...
My solution for this after reading couple of questions/solutions on stackoverflow :
Changed the default route to
routes.MapRoute(
name: "default",
template: $"{{parameter={settings.PrefixURL}}}/{{controller=Home}}/{{action=Index}}/{{id?}}");
where settings.PrefixURL => it's the some_parameter and it's value it's dynamic.
Problem i am facing =>
Doubled parameters in url, for example :
- https://hostername.com/ApplicationName/Home/Home/Profile
- https://hostername.com/ApplicationName/ApplicationName/FAQ
When tested locally that configuration of default routing worked perfectly, but after publishing the routing still works but the url is wrong.
What could be causing the issue?
Would creating Areas solve the routing to that kind of url?
Thank you.
What did the work for me was editing the path on web.config file and leaving the rest of soultion untouched.
From path="*" to path="/ApplicationName" where ApplicationName is the desired routing parameter.
As we know that routing follows the routes provided in RegisterRoutes function in RouteConfig.cs which defines the controller and action in default routes.
My project only have default route like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So how does routing detects that whether we are accessing some action or pointing to a file like image, css or js.
The short answer is that any request for a resource that has a . in its name is directly served by IIS without invoking MVC routing. Conversely, requests that do not contain a . are passed to MVC for routing. Importantly this is just default behaviour.
As a quick test, fabricate a url for any resource that does not exist in your project such as \asdf and note the 404 error generated by MVC. Now repeat the test with \asdf.asdf and note the 404 error is generated by IIS.
It is possible to change this behaviour with directives in web.config. For example, see How do I route images using ASP.Net MVC routing?
I would like to map routes to a controller action while passing a parameter as the subdomain, e.g http://subdomain.mydomain.com where "subdomain" is a parameter I'll be passing to the controller action for a database lookup. Is this achievable with Asp.Net Core 2.0 and how do I go about it?
You can use the Area which is a feature used to organize related functionality into a group as a separate routing-namespace for controller actions and folder structure for views.
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
This code is an example of a custom route for Area.
Maybe this link would help you Areas in ASP .NET Core
I want to display a default status page for my web api project (where instead some IIS message is displayed when I start the project). However it seems like I cannot create views in web api (there is no support for ActionResult).
In addition to a status page I will also use this information to create an api documentation page.
How can I achieve displaying html pages in this situation ?
If your default status page is static html, you don't have to use MVC. Just tell WebApi in your Startup.cs that you want to support static resources:
app.UseFileServer();
For creating an API documentation, maybe you could write that file on startup dynamically?
You can create regular controllers and views in a webAPI project the same as any MVC project. Just create a normal controller that does not inherit from ApiController. In your startup.cs make sure to configure at least a default route.
configuration.Routes.MapHttpRoute(
name: "someName",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Right click and select Add, then you should see controller at the top. Select one of the mvc controllers.
I have created a folder admin inside controller folder and wrote few controllers in it. I want to access all functions in that controllers in that folder by a url like abc.com/admin//.
I can get it working directly when its put directly in controller folder.
EDIT
WHAT HAVE YOU TRIED?
Just created a folder and wrote normal controllers in it. But I don't know how to route it.
Well, i would suggest, rather than just creating a folder and create controllers inside of it. You should create an Area
When you create an Area in asp.net mvc, it will automatically create folders(controller, model etc) for you. And the routing setup as well.
For more info, please visit
http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm
Add a route for the admin controllers in your route config if you want to support that path in the URL. Something like:
routes.MapRoute(
name: "Admin",
url: "admin/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But #DotNetDreamer is right that Areas are a better solution for the admin functionality of your site.