简体   繁体   中英

Case Sensitive Routing in MVC4

Does anyone know if it is possible to have Case Sensitive routing in MVC.

I would like as follows to point to different articles.

example

http://my.ie/uRl --> doc 1

http://my.ie/Url --> doc 2

Would love to hear if this can be done.

You could use Router Constraints and regular expressions like that:

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        new { id = @"\d+", action=@"[A-Z]+"  }  //Parameter constraints
        );

So, just think about constraints you want to apply in terms of regular expressions

Given MVC is an IIS platform (and windows itself is insensitive) I don't know if you're going to have much luck. However, if you're using it to serve up files you can create a fallback route that then takes it a step farther (and parses it appropriately). eg

routes.MapRoute(
  "File_Fetch",
  "{filename}",
  new { controller = "File", action = "Fetch", filename = UrlParameter.Optional },
  new { filename = @"^.+$" }
);

public class FileController : Controller
{
    public ActionResult Fetch(String filename)
    {
        // /Url -> filename = "Url"
        // /uRl -> filename = "uRl"
        return File(...);
    }
}

So now instead of trying to use the routing to differenciate cASe you can interrogate the incoming parameter and serve it up as you see fit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM