Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

asp.net mvc - MVC 4 Remove "home" from base route

Basically I want to make it so that: http://website.com/Home/About

Shows up as: http://website.com/About

The "home" controller showing up in the url would make the url longer for the user to read.

I tried to do the following:

routes.MapRoute(
            name: "About",
            url: "",
            defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
            );

Could someone help me out please?

question from:https://stackoverflow.com/questions/12828317/mvc-4-remove-home-from-base-route

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try something like this:

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...