属性与路由多项领先零小数 [英] Attribute Routing with Multiple Leading Zero Decimals

查看:103
本文介绍了属性与路由多项领先零小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前的ActionResult:

  [路线(EvaluatorSetup / {evalYear} / {}部门)]
公众的ActionResult RoutedEvaluatorSetup(INT evalYear,弦乐部)
{
    返回EvaluatorSetup((INT)evalYear,部门);
}

我想使用的网址:

  /EvaluatorSetup/2014/001.3244

其中{}部门是最终的字符串,然而,路由不捡{}部门作为一个字符串。

一个。我不知道是什么类型的MVC期待为001.3244,或者它是什么拿起它作为。

乙。我想保持它作为可选的前导零的字符串,如示例。

我在做什么错了?

更新:

我的意思是,当我把休息在我的code。在回线,它永远不会触发。

  / EvaluatorSetup / 2014 / foobar的(WORKS!)/EvaluatorSetup/2014/001.3244(不工作!)

这使我相信,我的路由是不正确的:

  [路线(EvaluatorSetup / {evalYear} / {}部门)]

具体而言,它似乎并不认为001.3244是一个有效的字符串。所以我的问题是我怎么解决这个:

  [路线(EvaluatorSetup / {evalYear} / {}部门)]
公众的ActionResult RoutedEvaluatorSetup(INT evalYear,弦乐部)

这样我可以输入网址:

  /EvaluatorSetup/2014/001.3244

preferably其中前导零被保留。

我想过这样的事情:

  [路线(EvaluatorSetup / {evalYear} / {公司} {}部门)]

然而,这是一个猜测。我甚至不知道这是否是有效的。

其他更新:

在RouteConfig.cs(这似乎不工作了)老路线是这样的:

  routes.MapRoute(
    evaluations_evaluatorsetupget
    评估/ evaluatorsetup / {evalyear} / {}部门,
    新{控制器=评估,行动=evaluatorsetup,evalyear = @^(\\ d {4})$,部门= @^(\\ d {3} \\。\\ D {4})$ },
    新{evalyear = @^(\\ d {4})$,部门= @^(\\ d {3} \\。\\ D {4})$}
    );


解决方案

的问题是,在URL中。

在默认情况下,如果是present的 StaticFileHandler 处理请求并查找一个文件名匹配的文件系统上的路径。要覆盖此行为,您可以指定一个处理程序,你要使用的URL。例如,添加以下到你的web.config:

 < system.webServer>
<&处理GT;
  <添加名称=UrlRoutingHandler路径=/ EvaluatorSetup / *动词=GETTYPE =System.Web.Routing.UrlRoutingHandler/>
< /处理器>
< /system.webServer>

将迫使任何要求开始与 / EvaluatorSetup / 使用 UrlRoutingHandler (与MVC路线相关的处理程序)

**解决方案**补充

我发现这个解决方案工作时,我还增加了以下内容web.config中的httpRuntime元素:

 <&的System.Web GT;
    <的httpRuntime relaxedUrlToFileSystemMapping =真/>
< /system.web>

Current ActionResult:

[Route("EvaluatorSetup/{evalYear}/{department}")]
public ActionResult RoutedEvaluatorSetup(int evalYear, string department)
{
    return EvaluatorSetup((int?)evalYear, department);
}

I would like to use the url:

/EvaluatorSetup/2014/001.3244

where the {department} is a ultimately a string, however, the routing is not picking up {department} as a string.

A. I don't know what type MVC is expecting for "001.3244", or what it is picking it up as.

B. I want to maintain it as a string with optional leading zeros, as in the example.

What am I doing wrong?

Update:

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}")]

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)

so that I can enter a uri:

/EvaluatorSetup/2014/001.3244

preferably where the leading zeros are maintained.

I thought about something like this:

[Route("EvaluatorSetup/{evalYear}/{corporation}.{department}")]

however, that is a guess. I don't even know if that is valid.

Additional Update:

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})$" }
    );

解决方案

The issue is the . in the URL.

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>

will force any request starting with /EvaluatorSetup/ to use the UrlRoutingHandler (the handler associated with MVC routes).

** Solution Supplement **

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屋!

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