ASP.NET 路由:标记之间的文字子段,以及使用文字子段中的字符路由值
我问的原因是因为 IIS 保护某些 ASP.NET 文件夹,如 Bin、App_Data、App_Code 等。即使 URL 没有映射到实际的文件系统文件夹,IIS 也会拒绝路径段等于以下之一的 URL提到的名字。
这意味着我不能有这样的路由:
{controller}/{action}/{id}
... 其中 id 可以是任何字符串,例如
Catalog/Product/Bin
因此,我愿意更改路由,在 id 之前使用后缀,而不是禁用此安全措施,如下所示:
{controller}/{action}_{id} // e.g. Catalog/Product_Bin
{controller}/{action}/_{id} // e.g. Catalog/Product/_Bin
但是这些路由如果 id 包含新的分隔符(在本例中为 _),则不起作用,例如
// These URL won't work (I get 404 response)
Catalog/Product_Bin_
Catalog/Product/_Bin_
Catalog/Product/__Bin
为什么?我不知道,对我来说看起来像一个错误。我怎样才能让这些路由工作,其中 id 可以是任何字符串?
The reason I'm asking is because IIS protects certain ASP.NET folders, like Bin, App_Data, App_Code, etc. Even if the URL does not map to an actual file system folder IIS rejects a URL with a path segment equal to one of the mentioned names.
This means I cannot have a route like this:
{controller}/{action}/{id}
... where id can be any string e.g.
Catalog/Product/Bin
So, instead of disabling this security measure I'm willing to change the route, using a suffix before the id, like these:
{controller}/{action}_{id} // e.g. Catalog/Product_Bin
{controller}/{action}/_{id} // e.g. Catalog/Product/_Bin
But these routes won't work if the id contains the new delimeter, _ in this case, e.g.
// These URL won't work (I get 404 response)
Catalog/Product_Bin_
Catalog/Product/_Bin_
Catalog/Product/__Bin
Why? I don't know, looks like a bug to me. How can I make these routes work, where id can be any string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我有一个明确的答案。是的,这是一个错误。然而,在这一点上,我很遗憾地说,我们没有计划修复它,原因如下:
您可以做的是将 URL 更改为不包含下划线:
然后添加一个路由约束,要求 ID 参数以下划线字符开头。
然后在您的操作方法中,您从 id 参数中删除下划线前缀。如果您愿意,您甚至可以编写一个操作过滤器来为您执行此操作。带来不便敬请谅解。
Ok, I have a definitive answer. Yes, this is a bug. However, at this point I regret to say we have no plans to fix it for a couple of reasons:
What you can do is change the URL to not have the underscore:
Then add a route constraint that requires that the ID parameter starts with an underscore character.
Then within your action method you trim off the underscore prefix from the id parameter. You could even write an action filter to do this for you if you liked. Sorry for the inconvenience.
您可以使用目录或文件名不允许的字符,例如:*、?、:、"、<、>、|。
You can use characters that are not allowed for a directory or file name like: *,?,:,",<,>,|.
对于 ASP.NET MVC,如果您查看源代码,它们具有路径分隔符 (/) 的硬编码值,据我所知,无法更改。
With ASP.NET MVC if you look at the source they have a hard-coded value for the path separator (/) and to my knowledge cannot be changed.