Omit controller name from URL mvc

人盡茶涼 提交于 2019-12-18 06:19:10

问题


I'm looking to do something similar to this post:

How to hide controller name in Url?

only without any sort of ID.

The server is running IIS 6 and the pages already show up without extensions so it's not a wildcard issue.

I'm looking to hit http://website.com/action-name

I have http://website.com/controller/action-name working

I'm assuming this is just a simple routing change that I am somehow goofing up. My current routing rule is:

routes.MapRoute(
    "RouteName",
"{action}",
new { controller = "Home", action = "Index" }
);

回答1:


Is your new routing rule positioned above the default routing rule of {controller, action, id} so that it has the opportunity to match first?




回答2:


The problem is your default route is still probably in place so it is matching it first and defaulting the rest of the inputs it expects. Based on your comment that the controller/action is working makes me think you didn't remove it or it is appearing first. Can you post your entire RegisterRoutes?

Try making the route you defined the very first route and it should match almost anything you pass at it.

EDIT: Added what your RegisterRoutes should look like:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // This will match anything so if you have something very specific with hard coded
    // values or more items that will need to be match add them here above but do not
    // add defaulted values so it can still fall through to this.
    routes.MapRoute( 
        "RouteName", 
        "{action}", 
        new { controller = "Home", action = "Index" });
}


来源:https://stackoverflow.com/questions/3169074/omit-controller-name-from-url-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!