MVC路由没有在url控制器 [英] MVC Routing without controller in the url

查看:165
本文介绍了MVC路由没有在url控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何配置ASP.NET MVC 3路由,因此它不会显示在URL中的控制器?

下面是我的路线

  routes.MapRoute(
            默认,
            {控制器} / {行动} / {ID}
            新{控制器=家,行动=索引,ID = UrlParameter.Optional}
        );        routes.MapRoute(
            HomeActions
            {行动},
            新{行动=公司简介}
        );

我需要网址:

  mysite.com/AboutUs

但我有

  mysite.com/Home/AboutUs


解决方案

我会非常具体,你要路由的URL。并把它的默认路由之上。

  routes.MapRoute(
        HomeActions
        关于我们,
        新{控制器=家,行动=公司简介}
    );    routes.MapRoute(
        默认,
        {控制器} / {行动} / {ID}
        新{控制器=家,行动=索引,ID = UrlParameter.Optional}
    );

正与像你的建议可能有不良后果的一个途径不太具体。特别是如果列出的默认路由的下面。

  routes.MapRoute(
    HomeActions
    {行动},
    新{控制器=家,行动=公司简介}
);

例如,如果上面的路由缺省后添加然后链接 http://www.example.com /公司简介很可能会匹配的路由{控制器=公司简介,行动=索引,ID = UrlParamter.Optional}。如果添加默认的上述路线,然后寻找网址 http://www.example.com/Users 你可能想成为用户控制器现在看起来适用于家庭的控制器上的用户操作的指数操作。

所以,我建议是具体说明这样的路线。

How do I configure ASP.NET MVC 3 routing so it doesn’t shown the controller in the url?

Here's my routes

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

        routes.MapRoute(
            "HomeActions", 
            "{action}", 
            new { action= "AboutUs" } 
        );

I need url:

mysite.com/AboutUs

But I have

 mysite.com/Home/AboutUs

解决方案

I would be very specific about the url that you want to route. And place it above the default route.

    routes.MapRoute(
        "HomeActions", 
        "AboutUs", 
        new { controller = "Home", action= "AboutUs" } 
    );

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

Being less specific with a route like the one you suggested might have unwanted consequences. Especially if listed below the default route.

routes.MapRoute(
    "HomeActions", 
    "{action}", 
    new { controller = "Home", action= "AboutUs" } 
);

For example, if the above route is added after the default then the url http://www.example.com/AboutUs would likely match the route {controller = "AboutUs", action = "Index", id = UrlParamter.Optional}. If you added the route above the default one, then looking for the url http://www.example.com/Users which you might want to be the Index action on the Users controller would now look for the Users action on the Home controller.

So, I would advise being specific about routes like that.

这篇关于MVC路由没有在url控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆