MVC4 project - cannot have dot in parameter value?
Asked Answered
O

4

5

I have an MVC4 project, and I am trying to get it working on URLs like /QRCode/address/amount. Here's how it is declared:

Route:

routes.MapRoute(
    name: "QRCode",
    url: "QRCode/{address}/{amount}",
    defaults: new { controller = "QRCode", action = "Index" }
);

Controller:

public class QRCodeController : Controller
{
    public ActionResult Index(string address, double amount)
    {
         ...

The problem is:

When URL is: QRCode/address1/33, all works fine, but if there is a dot in second parameter, such as: QRCode/address1/33.33, I am getting a "HTTP Error 404.0 - Not Found".

Re-declaring second parameter a string yields same result.

Using %2E in lieu of a dot yields same result

Anybody knows what is going on here? I know it worked fine in MVC3

Organism answered 16/12, 2012 at 19:34 Comment(2)
"dots" usually trigger IIS to try and map the file to a MIME type, then to a handler. So IIS is probably looking for a .33 handler, which of course doesn't exist. Is your web.config configured to run all requests through the pipeline?Glazing
I would check handler mappings..Kennykeno
H
7

if this is on IIS 7, then add this to your config file and it should work fine:

<system.web>
     <httpRuntime relaxedUrlToFileSystemMapping="true" />
</system.web>
Heretic answered 16/12, 2012 at 19:58 Comment(1)
I tested, and it does indeed do the trick. Apparently, what changed was not MVC version, but rather runtime. My httpRuntime tag had targetFramework="4.5", this is what caused the issue. relaxedUrlToFileSystemMapping fixed it.Organism
O
6

Yes... See comments, the handler mapping was a problem.

I changed URL from QRCode/address1/33.33 to QRCode/address1/33.33/ and mapping worked fine

Organism answered 16/12, 2012 at 19:43 Comment(0)
L
0

Here's another option: don't map the amount but pass it as a URL parameter with name:

routes.MapRoute(
    name: "QRCode",
    url: "QRCode/{address}",
    defaults: new { controller = "QRCode", action = "Index" }
);

now call the api with such an url:

http://<server>/QRCode/address1?amount=33.33
Legislator answered 27/6, 2014 at 17:59 Comment(0)
W
0

I have same issue using MVC .NET 4.7.2

but solved by adding these configuration to web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
    </modules>
  </system.webServer>
Winniewinnifred answered 3/11 at 22:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.