ASP.NET MVC 3 - 区域路由的边缘情况

发布于 2024-11-25 11:39:10 字数 879 浏览 3 评论 0原文

我有一个页面,用户可以在其中针对产品“提出问题”。

路线是:

context.MapRoute(
                "Q&A - Ask - by Product",
                "{someProductUri}/questions/ask",
                new { controller = "Questions", action = "Ask" }
            );

匹配:

/汽车/问题/询问

/电话/问题/询问

操作如下:

public class QuestionsController : Controller
{
    public ViewResult Ask(string someProductUri)
    {
      // ...
    }
}

现在,问题是,我还需要允许用户在页面本身上选择产品,例如,在 URI 中没有预先选择产品。

因为该控制器位于区域中,所以该“默认 URL”将如下所示:

/myarea/问题/询问

那么发生的事情是,当我开始执行操作时,“someProductUri”被设置为“myarea”,导致各种悲伤。

我想对两组 URL 重复使用相同的操作/视图,但我不希望在默认 URL 时将路由值设置为“myarea”。

希望这是有道理的——有什么想法吗?

我需要单独的路线吗?我可以添加一个路由约束,使“someProductUri”不能是“myarea”,这样它就与路由不匹配并回退到默认值吗?

I have a page where user's can "Ask a Question" against a product.

The route is:

context.MapRoute(
                "Q&A - Ask - by Product",
                "{someProductUri}/questions/ask",
                new { controller = "Questions", action = "Ask" }
            );

Which matches:

/car/questions/ask

/phone/questions/ask

Here's the action:

public class QuestionsController : Controller
{
    public ViewResult Ask(string someProductUri)
    {
      // ...
    }
}

Now, the problem is, i also need to allow the user to select the product on the page itself, e.g with no product pre-chosen in the URI.

Because this controller is in an area, this "default URL" will be like this:

/myarea/questions/ask

So what's happening is, when i get to the action, "someProductUri" is set to "myarea", causing all sorts of grief.

I want to re-use the same action/view for both sets of URL's, but i don't want the route value to be set to "myarea" when it's the default URL.

Hope that makes sense - any ideas?

Do i need a seperate route? Can i add a route constraint so that the "someProductUri" can't be "myarea", so that it doesn't match the route and falls back to the default?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-12-02 11:39:11

我已经有太多路线,所以我不想仅针对这种边缘情况添加另一条路线。

所以我最终使用了一个路由约束:

public class NotEqualToAreaName : IRouteConstraint
{
   public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
   {
      return String.Compare(values[parameterName].ToString(), route.DataTokens["area"].ToString(), true) != 0;
   }    
}

非常简单,而且通用 - 所以我可以在遇到这种类型的边缘情况时随时重复使用它。

I already have too many routes, so i didn't want to add another just for this edge case.

So i ended up using a route constraint:

public class NotEqualToAreaName : IRouteConstraint
{
   public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
   {
      return String.Compare(values[parameterName].ToString(), route.DataTokens["area"].ToString(), true) != 0;
   }    
}

Pretty simple, and generic - so i can re-use it anytime i come across this type of edge case.

去了角落 2024-12-02 11:39:11

您需要为该区域添加单独的路线:

context.MapRoute(
            "Q&A - Ask - by Product",
            "myarea/questions/ask",
            new { controller = "Questions", action = "Ask" }
        );

You need to add a separate route for the area:

context.MapRoute(
            "Q&A - Ask - by Product",
            "myarea/questions/ask",
            new { controller = "Questions", action = "Ask" }
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文