出于某种原因,每当我尝试在本地安装的ASP.NET MVC3中使用参数解析URL时,参数基本上都会出现null在我的控制器的处理函数中.
例如,我有
public class HomeController : Controller
{
public ActionResult Foo(string bar)
{
ViewBag.Message = bar;
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
并尝试访问http://localhost/myapp/foo/sometext或http://localhost/myapp/home/foo/sometext,bar基本上评估null而不是sometext.
我相信我的MVC3安装工作正常,因为我已经设法在几天前运行一个带有自定义路由规则的单独应用程序.我有点担心我可能会在某处或其他地方搞砸配置标志.
关于这里可能出现什么问题的任何想法?
默认路由映射需要id在操作中命名的参数.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
如果您将操作更改为:
public ActionResult Foo(string id)
Run Code Online (Sandbox Code Playgroud)
它应该工作.如果无法更改参数名称,您也可以尝试这样的URL:
http://localhost/myapp/home/foo/?bar=sometext
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4568 次 |
| 最近记录: |