设置索引作为用于控制器的默认路由 [英] Setting up Index as the default route for a controller

查看:112
本文介绍了设置索引作为用于控制器的默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址

  • http://www.roadkillwiki.org/Page/Index/documentation

我要变成

这也可能是类似<一个href=\"http://www.roadkillwiki.org/Page/my-url-with-spaces\">http://www.roadkillwiki.org/Page/my-url-with-spaces - 参数是字符串。这条路线的设置我已经试过是:

That could also be something like http://www.roadkillwiki.org/Page/my-url-with-spaces - the parameter is a string. The route setup I've tried is:

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

然而,这与默认的ID路线干扰了MVC项目配备。是否有实现这一目标的方法吗?

However this is interfering with the default "id" route that MVC projects come with. Is there any way of achieving this?

推荐答案

您不必失去缺省路由。为了避免你的路线相互干扰,关键是要命令他们这样更具体的规则precede不太具体的。例如:

You don't need to lose the default route. The key to avoiding your routes interfere with each other is to order them so the more specific rules precede the less specific ones. For example:

// Your specialized route
routes.MapRoute(
    "Page",
    "Page/{slug}",
    new { controller = "Page", action = "Index" }
);

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

那么你的PageController是这样的:

Then your PageController would look like this:

using System.Web.Mvc;

public class PageController : Controller
{
    public string Index(string slug)
    {
        // find page by slug
    }
}

这是说,我的强烈建议你这样做,而不是:

That said, I would strongly advice you to do this instead:

// Your specialized route
routes.MapRoute(
    "Page",
    "Page/{id}/{slug}",
    new { controller = "Page", action = "Index", slug = UrlParameter.Optional }
);

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

和您的PageController:

And your PageController:

using System.Web.Mvc;

public class PageController : Controller
{
    public string Index(int id)
    {
        // find page by ID
    }
}

通过包括您的网址开头的页面ID是(像计算器一样)或者结尾,然后你可以忽略蛞蝓,而是通过ID检索您的网页。如果用户更改页面名称这将为你节省一吨头痛。我已经通过这个走了,这是痛苦的;基本上你必须保持你的页面都在过去的所有名称的记录,只是让你的访客/搜索引擎不得到404每次页面被重命名的时间。

By including the page ID either at the beginning of your URL (like StackOverflow does) or at the end, you can then just ignore the slug, and instead retrieve your pages by ID. This will save you a ton of headaches if your users change the page name. I have gone through this and it's painful; you basically have to keep a record of all names your pages have had in the past, just so your visitors/search engines don't get a 404 every time a page is renamed.

希望这有助于。

这篇关于设置索引作为用于控制器的默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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