ASP.NET 路由:标记之间的文字子段,并使用文字子段中的字符路由值 [英] ASP.NET routing: Literal sub-segment between tokens, and route values with a character from the literal sub-segment

查看:21
本文介绍了ASP.NET 路由:标记之间的文字子段,并使用文字子段中的字符路由值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问的原因是因为 IIS 保护某些 ASP.NET 文件夹,如 Bin、App_Data、App_Code 等.即使 URL 未映射到实际的文件系统文件夹 IIS 拒绝路径段等于的 URL到提到的名字之一.

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}

... 其中 id 可以是任何字符串,例如

... where id can be any string e.g.

Catalog/Product/Bin

因此,我愿意更改路由,而不是禁用此安全措施,在 id 之前使用后缀,如下所示:

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

但如果 id 包含新的分隔符,则这些路由将不起作用,在这种情况下为 _,例如

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

为什么?我不知道,对我来说似乎是一个错误.我怎样才能使这些路由工作,其中 id 可以是任何字符串?

Why? I don't know, looks like a bug to me. How can I make these routes work, where id can be any string?

推荐答案

好的,我有一个明确的答案.是的,这是一个错误.但是,在这一点上,我很遗憾地说我们没有计划修复它,原因有两个:

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:

  • 这是一个重大变化,可能很难被注意到.
  • 有一个简单的解决方法.

您可以做的是将 URL 更改为不包含下划线:

What you can do is change the URL to not have the underscore:

{controller}/{action}/_{id}

然后添加一个路由约束,要求ID参数以下划线字符开头.

Then add a route constraint that requires that the ID parameter starts with an underscore character.

然后在您的操作方法中,您从 id 参数中删除下划线前缀.如果您愿意,您甚至可以编写一个动作过滤器来为您执行此操作.很抱歉给您带来不便.

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.

这篇关于ASP.NET 路由:标记之间的文字子段,并使用文字子段中的字符路由值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆