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?
Related
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.
I moved a solution from one machine to another and am having nightmares. I've got one problem left. The default page can't get served, it says Resource cannot be found. I have in the routeConfig.cs
routes.MapRoute("Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I get Resource cannot be found.
I can access the default page by :-
http://example.com/home/index
The other questions don't seem to answer it.
EDIT: If I put the above line of code at the top of the Routing code, the default page works but other pages don't. If I put the code at the bottom, other pages work but the default page doesn't.
Try look in properties there is "Web" tab were you cat chose server and project URL, hope
If you have only default routing on RouteConfig file in development machine and keep getting 404, it seems that your problem comes from project's virtual path settings.
http://forums.asp.net/t/1893154.aspx?The+resource+cannot+be+found+404
If application is deployed on local host (or you have set Use local IIS
Web Server) then it means you have created a virtual directory named
"YourSite".
Right click your app project on VS, choose Properties, then go to "Web" part and set your site's virtual path on "Project Url".
For virtual directory problem on live IIS server, see here to add virtual directory into destination machine before deploying your app.
In addition, if you have custom routing rule, make sure the custom route placed properly on top of default routing rule.
The problem was that I had two Config Rules pointing to the same address. One of the other rules was pointing to the wrong controller. The wrong controller was erroring because I had set up the action, I had used the name of the controller as the action, thats a no-no. The above rule was correct but the incorrect rule was
routes.MapRoute(
name: "pagelist",
url: "pagelist",
defaults: new { controller = "pagelist", action = "pagelist", id = UrlParameter.Optional }
);
the action should have been Index.
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.
I have to deal with a legacy asp.net mvc app, which is not configured as I am used to. After a normal logout via:
FormsAuthentication.SignOut();
and
return RedirectToAction("Index", "Home");
the URI contains:
ReturnUrl=%2f
This is not usually the case. How can I suppress this?
Alternatively, when I try to access a page that requires authentication/authorization the login page appears but no appropriate ReturnUrl= is generated (i.e. the URI stays as it is).
Is this an IIS issue, which I have read somewhere, or is the asp.net FormsAuthenticationModule not properly configured? Thanks.
ReturnUrl is added during an unauthorized redirect. Someone appears to be redirecting the root url (/) to itself or to Home/Index. %2f is the encoded form of "/".
I would check the authorization section of the web.config for something that looks wrong.
This is depends on you routing, right? If you have this as you last route configuration:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
If nothing works then add authentication mode="Windows" in your system.web attribute in your Web.Config file. hope it will work for you.