How can I write a route to match on just an HTML file name?
I have an ASP.Net MVC 2 application that serves up static HTML files as an actionresult
Content(htmlString);
This HTML contains relative links within it. i.e. /absdk123.html?t1=somevalue&t2=someothervalue
What should my route look like to match these HTTP requests? I have t开发者_如何转开发ried this:
routes.MapRoute(
"LinkRoute", // Route name
"{html}", // URL with parameters
new { controller = "Home", action = "ResolveLink"} // Parameter defaults
);
At the action, "ResolveLink" the string parameter is always 'null'. How can I write a route to correctly handle these links?
public ActionResult ResolveLink(string htmlFileName)
{
//test param
return Content(htmlFileName);
}
Try this:
routes.MapRoute(
"LinkRoute", // Route name
"{htmlFileName}", // URL with parameters
new { controller = "Home", action = "ResolveLink"} // Parameter defaults
);
public ActionResult ResolveLink(string htmlFileName)
{
//test param
return Content(htmlFileName);
}
routes.MapRoute(
"LearningModuleLessonRoute", // Route name
"{html}.html", // URL with parameters
new { controller = "Home", action = "ResolveLink" }// Parameter defaults
);
This will actually match any request of format /{filename}.html. I didn't realize that a file extension could be included in a route.
精彩评论