Attribute Routing with Multiple Leading Zero Decim

2020-04-11 11:40发布

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

2条回答
劫难
2楼-- · 2020-04-11 11:52

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>
查看更多
冷血范
3楼-- · 2020-04-11 12:06

Try

[Route("EvaluatorSetup/{evalYear}/{department:string}")]

I believe the default is int.

查看更多
登录 后发表回答