ASP.NET MVC 3 - 区域路由的边缘情况
我有一个页面,用户可以在其中针对产品“提出问题”。
路线是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经有太多路线,所以我不想仅针对这种边缘情况添加另一条路线。
所以我最终使用了一个路由约束:
非常简单,而且通用 - 所以我可以在遇到这种类型的边缘情况时随时重复使用它。
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:
Pretty simple, and generic - so i can re-use it anytime i come across this type of edge case.
您需要为该区域添加单独的路线:
You need to add a separate route for the area: