問題描述
我問的原因是因?yàn)?IIS 保護(hù)某些 ASP.NET 文件夾,如 Bin、App_Data、App_Code 等.即使 URL 未映射到實(shí)際的文件系統(tǒng)文件夾 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?
推薦答案
好的,我有一個明確的答案.是的,這是一個錯誤.但是,在這一點(diǎn)上,我很遺憾地說我們沒有計(jì)劃修復(fù)它,原因有兩個:
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參數(shù)以下劃線字符開頭.
Then add a route constraint that requires that the ID parameter starts with an underscore character.
然后在您的操作方法中,您從 id 參數(shù)中刪除下劃線前綴.如果您愿意,您甚至可以編寫一個動作過濾器來為您執(zhí)行此操作.很抱歉給您帶來不便.
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.
這篇關(guān)于ASP.NET 路由:標(biāo)記之間的文字子段,并使用文字子段中的字符路由值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!