具有多个前导零小数的属性路由 [英] Attribute Routing with Multiple Leading Zero Decimals
问题描述
当前操作结果:
[Route("EvaluatorSetup/{evalYear}/{department}")]
public ActionResult RoutedEvaluatorSetup(int evalYear, string department)
{
return EvaluatorSetup((int?)evalYear, department);
}
我想使用网址:
/EvaluatorSetup/2014/001.3244
其中 {department} 最终是一个字符串,但是,路由不会选择 {department} 作为字符串.
where the {department} is a ultimately a string, however, the routing is not picking up {department} as a string.
A.我不知道 MVC 期望001.3244"是什么类型,也不知道它是什么类型的.
A. I don't know what type MVC is expecting for "001.3244", or what it is picking it up as.
B.我想将其保留为带有可选前导零的字符串,如示例中所示.
B. I want to maintain it as a string with optional leading zeros, as in the example.
我做错了什么?
更新:
我的意思是,当我在返回行中断代码时,它永远不会触发.
What I mean is, when I put a break in my code at the return line, it never fires.
/EvaluatorSetup/2014/foobar (WORKS!)
/EvaluatorSetup/2014/001.3244 (DOESN'T WORK!)
这让我相信我的路由不正确:
This leads me to believe that my routing is not correct:
[Route("EvaluatorSetup/{evalYear}/{department}")]
具体来说,001.3244 似乎不是一个有效的字符串.所以我的问题是如何纠正这个:
Specifically, it doesn't seem that 001.3244 is a valid string. So my question is how do I correct this:
[Route("EvaluatorSetup/{evalYear}/{department}")]
public ActionResult RoutedEvaluatorSetup(int evalYear, string department)
这样我就可以输入一个uri:
so that I can enter a uri:
/EvaluatorSetup/2014/001.3244
最好保留前导零.
我想过这样的事情:
[Route("EvaluatorSetup/{evalYear}/{corporation}.{department}")]
然而,这是一个猜测.我什至不知道这是否有效.
however, that is a guess. I don't even know if that is valid.
其他更新:
RouteConfig.cs 中的旧路由(似乎不再起作用)是这样的:
the old route in the RouteConfig.cs (which doesn't seem to work anymore) is this:
routes.MapRoute(
"evaluations_evaluatorsetupget",
"evaluations/evaluatorsetup/{evalyear}/{department}",
new { controller = "evaluations", action = "evaluatorsetup", evalyear = @"^(d{4})$", department = @"^(d{3}.d{4})$" },
new { evalyear = @"^(d{4})$", department = @"^(d{3}.d{4})$" }
);
推荐答案
问题在于 URL 中的 .
.
The issue is the .
in the URL.
默认情况下,如果 .
存在,StaticFileHandler
会处理请求并查找与文件系统上的路径匹配的文件名.要覆盖此行为,您可以为您尝试使用的 URL 分配一个处理程序.例如,将以下内容添加到您的 web.config:
By default, if a .
is present the StaticFileHandler
handles the request and looks for a filename matching the path on the file system. To override this behavior, you can assign a handler to a URL you are trying to use. For example, adding the following to your web.config:
<system.webServer>
<handlers>
<add name="UrlRoutingHandler" path="/EvaluatorSetup/*" verb="GET" type="System.Web.Routing.UrlRoutingHandler" />
</handlers>
</system.webServer>
将强制任何以 /EvaluatorSetup/
开头的请求使用 UrlRoutingHandler
(与 MVC 路由关联的处理程序).
will force any request starting with /EvaluatorSetup/
to use the UrlRoutingHandler
(the handler associated with MVC routes).
** 解决方案补充 **
** Solution Supplement **
我发现当我还向 web.config 中的 httpRuntime 元素添加以下内容时,此解决方案有效:
I found that this solution worked when I ALSO added the following to the httpRuntime element in web.config:
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
</system.web>
这篇关于具有多个前导零小数的属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!