简体   繁体   English

在区域中使用两个参数的MVC5路由

[英]MVC5 Routing with two parameters in an Area

I have an area called News, and within that area I have a Post Controller. 我有一个称为新闻的区域,在该区域内有一个后控制器。 The post controller has the following action: 后控制器具有以下操作:

public ActionResult Index(int id, string name)
{
...
}

I have a route in the NewsAreaRegistration 我在NewsAreaRegistration中有一条路线

    context.MapRoute(
        "News_post",
        "News/{controller}/{id}/{name}",
        new { action = "Index", controller = "Post",name = UrlParameter.Optional }, new { id = @"\d+"}
    );      

I have also tried without the name object attribute, with and without the id @"\\d+ .. Thing is I have this on another site with the exact same setup, just so confused at why it's not working as expected. 我也尝试过不使用name对象属性,使用和不使用id @“ \\ d +.。在另一个站点上,我使用完全相同的设置进行了尝试,只是对为什么它无法按预期工作感到困惑。

Now firstly, the action will resolve: 现在首先,该操作将解决:

http://example.com/News/Post/Index/3 

When I want it to resolve to 当我想解决

http://example.com/News/Post/3 

And then I also want this string parameter at the end so it should resolve to 然后,我也希望最后使用此字符串参数,因此它应该解析为

http://example.com/News/Post/3/test-post

but instead resolves to 但改为解决

http://example.com/News/Post/Index/3?test-post

I am having a total nightmare with this routing stuff. 我对这个路由东西感到完全的噩梦。 I have tried to mess around with the Routing attributes but also have no luck there with areas... Any ideas guys? 我试图弄乱“路由”属性,但在区域方面没有运气...有什么主意吗?

So I fixed part of the issue by making the Route look like this: 因此,我通过使“路线”看起来像这样来解决了部分问题:

  context.MapRoute( "News", "News/Post/{id}/{name}", new { action = "Index", controller = "Post" }, new { id = @"\\d+" } ); 

So I removed the UrlParamter.Optional for the name argument. 所以我删除了name参数的UrlParamter.Optional。 I need to really get my head around how some of these routes work. 我需要真正弄清楚其中一些路线的工作方式。 At times it is so easy, but then others I can't get it to do the simplest things. 有时它是如此简单,但是其他人却无法用它来做最简单的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM