MVC 3 - 路线中的斜杠和空格
我在 Global.asax(MVC 3 Web 项目)中定义了以下路由:
routes.MapRoute(
"BlogCategory", // Route name
"Blog/Category/{*category}", // URL with parameters
new { controller = "Blog", action = "Index", category = "" } // Parameter defaults
);
我的操作接受类别参数,如下所示:
public ViewResult Index(string category, int page = 1)
{
PostListViewModel viewModel;
if (string.IsNullOrEmpty(category))
{
....show all cats else show only the one passed in
这工作正常,并将类别传递到控制器,适当地过滤我的结果。
我的问题是,当我创建的类别之一的类别名称如下所示时:
项目/实验室
(注意空格和斜杠)
这将创建一个类似如下的 URL:
/博客/类别/项目%20/%20Lab
当我点击链接时,出现以下错误:
描述:HTTP 404。您正在查找的资源(或其其中之一) 依赖项)可能已被删除,其名称已更改,或者是 暂时无法使用。请检查以下 URL 并进行 确保拼写正确。
请求的 URL:/Blog/Category/Projects/Lab
调试时永远不会到达索引操作。
我的问题是,如何才能完成这项工作,或者在创建类别名称时必须进行一些输入验证以防止这种情况发生?
I have the following route defined in my Global.asax (MVC 3 web project):
routes.MapRoute(
"BlogCategory", // Route name
"Blog/Category/{*category}", // URL with parameters
new { controller = "Blog", action = "Index", category = "" } // Parameter defaults
);
And my action accepts a category parameter, looking like this:
public ViewResult Index(string category, int page = 1)
{
PostListViewModel viewModel;
if (string.IsNullOrEmpty(category))
{
....show all cats else show only the one passed in
This works fine and will pass the category through to the controller, filtering my results appropriately.
My problem is when one of the categories I created looks like this for it's category name:
Projects / Lab
(note the spaces and slash)
This creates a URL similar like this:
/Blog/Category/Projects%20/%20Lab
And when I follow the link, I get this error:
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make
sure that it is spelled correctly.Requested URL: /Blog/Category/Projects / Lab
It never reaches the index action when debugging.
My question is, how can I make this work, or must I do some input validation when creating category names to prevent this from occurring?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Tyrsius 建议的那样,传递类别 ID 会更容易,但如果您想传递名称,那么我建议每次创建链接或创建自定义
时使用
为您做这件事。一旦它命中您的控制器操作,只需执行Url.Encode()
>UrlHelperUrl.Decode(
) 即可取回原始字符串。更简洁的方法是创建您自己的路由处理程序(实现
IRouteHandler
)来为您完成此操作。As Tyrsius suggested it will be easier to pass the category ID around but if you want to pass the name then I would suggest using
Url.Encode()
every time you create a link or create a customUrlHelper
to do it for you. Once it hits your controllers action just do aUrl.Decode(
) to get the original string back.A more cleaner way would be to create your own route handler (implement
IRouteHandler
) to do it for you.首先,感谢 Tyrsius、Romias 和 eth0 的建议。
我决定不想将 Id 用于类别,也不想创建路由处理程序,因为这并不能真正解决根本问题。
相反,我创建了一个名为 "UsedAsUrl" 的验证属性,并将其应用于域模型中的 Category.Name。
从开发人员的角度来看,这具有内置验证(对最终用户有利)和良好的可重用性的好处。
所以我的类别模型现在看起来像这样(注意 [UsedAsUrl] 属性):
我创建的属性看起来像这样:
现在当我去添加类别时:
我自动收到此响应:
JS 尚未工作,但我的控制器可以获取模型状态,这是它的响应,因此该属性工作正常。
这基本上检查任一侧/任意一侧是否有空格的斜杠。请注意,这不是一个详尽的 URL 验证器,但它适用于我当前的项目。如果有人有任何改进,请告诉我,我会修改这个答案。
我不擅长 RegExp,因此没有从 RegularExpressionAttribute 派生,而且我也不想从模型内部提供错误消息。这个属性确实应该建立在使用类别作为 URL 部分时出现更多规则的基础上。
First off, thanks to Tyrsius, Romias and eth0 for their suggestions.
I decided that I didn't want to use the Id for a category and I didn't want to create a route handler as this isn't really solving the root issue.
Instead I create a validation attribute called "UsedAsUrl", and applied this to my Category.Name in my domain model.
This has the benefits of inbuilt validation (good for the end user) and good re-usability from a developer perspective.
So my category model now looks like this (note the [UsedAsUrl] attribute):
And the attribute I created looks like this:
Now when I go to add a category:
I get this response automatically:
JS doesn't yet work, but my controller can pick up on the model state and this is the response from it, so the attribute is working correctly.
This basically checks for a slash with spaces on either / any side. Please note that this is not an exhaustive URL validator, but it will do for my current project. If anyone has any improvements, please let me know and I'll amend this answer.
I'm not good with RegExp, so didn't derive from RegularExpressionAttribute, and also I didn't want to have to supply the error message from inside my model. This Attribute should really be built upon as more rules appear when using categories as URL parts.