Asp.net mvc ASP MVC5方法未命中
我在EventsController中有一个具有以下定义的方法:Asp.net mvc ASP MVC5方法未命中,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我在EventsController中有一个具有以下定义的方法: public JsonResult Index(string id) { ... } 当我试图使用浏览器访问它时,我无法访问它。但当我浏览到时,我的调试点被命中,id为null。为什么呢?我的路线定义如下(我没有更改): 我设法解决了它,这是一件简单的事情。而不是使用http://localhost:57715/events/some_string我应该使用http://localhost:57715/events/In
public JsonResult Index(string id)
{
...
}
当我试图使用浏览器访问它时,我无法访问它。但当我浏览到时,我的调试点被命中,id为null。为什么呢?我的路线定义如下(我没有更改):
我设法解决了它,这是一件简单的事情。而不是使用
http://localhost:57715/events/some_string
我应该使用http://localhost:57715/events/Index/some_string
当您请求http://localhost:57715/events/some_string
,MVC框架不知道some_string
是操作方法名称还是id参数值。因此,您应该显式地指定参数名
这应该行得通
http://localhost:57715/events?id=some_string
或您可以使用包含控制器名称和操作方法名称以及id参数值的url
http://localhost:57715/events/yourActionMethodName/some_string
如果你想让URL <代码>你的站点/事件/某个字符串工作,你可以考虑启用属性路由,并在你的动作方法中指定一个类似上面的路由模式,如下面的
public class EventsController : Controller
{
[Route("Events/{id}")]
public ActionResult Index(string id)
{
return Content("id : "+id);
}
}
现在,请求yourSite/events/some_string
将由EventsController的索引操作方法处理
public class EventsController : Controller
{
[Route("Events/{id}")]
public ActionResult Index(string id)
{
return Content("id : "+id);
}
}