Weird behavior of urlrewriting in MVC3 with razor viewengine
I am working on a project which adopted ASP.NET MVC3(Razor) tech. Now, I have a controller below:
public class Home: Controller
{
public ActionResult Result(string id)
{
return View(id);
}
}
and I have set MapRoute as below:
context.MapRoute(
"Home_result",
"Home/Result/{id}",
new { controller="Home", action = "Result"}
);
and it was suposed to display a View which named "Result" when I typed the url http://domain.com/Home/Result/abc123 in the browser. However it didn't. Instead it gave me an exception below:
The view 'Result' or i开发者_如何学Pythonts master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/abc123.cshtml
~/Views/Home/abc123.vbhtml
~/Views/Shared/abc123.cshtml
~/Views/Shared/abc123.vbhtml
It is strange isn't it? Can anyone help me to figure out what mistakes I've made?
return View(id);
Returns a view with the name of ID's value (.cshtml), not the view with the name result.cshtml. I think this is because Id is a string. Are you trying to pass the id to the view?
To return the view matching the name of your controller's action simply use
return View();
If you want to pass that value to the view, for what ever crazy reason, using the viewbag is the easiest way since the string id is being used to declare a view name.
ViewBag.ID = id;
return View();
Then in the view just use the value you stored. And yes Razor HTML encodes by default.
@ViewBag.ID
精彩评论