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
380 views
in Technique[技术] by (71.8m points)

.net - How to remove controller name from URL in MVC project

I have a URL like below and I need to remove the controller name (myController). I've use several fixes but none fixed the issue. Please help me guys..

http://foldername.example.com/foldername/myController/my-page

'example.com' is the domain and all the files related to the site is inside 'foldername' folder. 'my-page' is the view name

In the end I need the above URL to be like below.

http://example.com/my-page

Thank in advance guys..!!

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You could try inverting the routes definition by placing the more specialized route first. Also you probably didn't want to hardcode the action name as action but rather use the {action} placeholder:

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

    routes.MapRoute(
        name: "Special",
        url: "{action}",
        defaults: new { controller = "Home", action = "LandingIndex" }
    );

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

Follow the below links :

Link 1

Link 2

Link 3


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

...