简体   繁体   English

ASP MVC路由没有操作

[英]ASP MVC Routes without actions

I would like to leave out the action in url as I do not consider that to be a restful approach. 我想在url中省略这个动作,因为我不认为这是一个宁静的方法。 Default routes should be: 默认路由应为:

"{controller}/{id}"

And then call the action that corresponds to the HTTP method used. 然后调用与所使用的HTTP方法相对应的操作。 For example I am decorating a PUT action like thus: 例如,我正在装饰像这样的PUT动作:

[HttpPut]
public ActionResult Change()
{
    return View();
}

However when cUrl-ing this, I get a 404. So I'm doing something wrong, any one tried this approach before? 然而,当我这样做时,我得到了404.所以我做错了,之前有人试过这种方法吗?

I'm using the MVC4 beta. 我正在使用MVC4测试版。

This is all I'm doing to set up the Routes: 这就是我正在设置路由的全部内容:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{id}",
        defaults: new { controller = "Home", action = "Index", id = RouteParameter.Optional }
    );
[HttpPut]
[ActionName("Index")]
public ActionResult Change()
{
    return View();
}

The action method selector in MVC will only allow you to have at most, 2 action method overloads for the same-named method. MVC中的操作方法选择器只允许您为同名方法最多使用2个操作方法重载。 I understand where you are coming from with wanting to only have {controller}/{id} for the URL path, but you may be going about it in the wrong way. 我想知道你来自哪里,想要只有{controller} / {id}作为URL路径,但你可能会以错误的方式解决它。

If you only have 2 action methods for your controller, say 1 for GET and 1 for PUT, then you can just name both of your actions Index, either like I did above, or like this: 如果您的控制器只有2个操作方法,比如说GET为1,PUT为1,那么您可以像上面一样命名两个操作索引,或者像这样:

[HttpPut]
public ActionResult Index()
{
    return View();
}

If you have more than 2 methods on the controller, you can just create new custom routes for the other actions. 如果控制器上有两个以上的方法,则可以为其他操作创建新的自定义路由。 Your controller can look like this: 您的控制器可能如下所示:

[HttpPut]
public ActionResult Put()
{
    return View();
}

[HttpPost]
public ActionResult Post()
{
    return View();
}

[HttpGet]
public ActionResult Get()
{
    return View();
}

[HttpDelete]
public ActionResult Delete()
{
    return View();
}

... if your global.asax looks like this: ...如果你的global.asax看起来像这样:

routes.MapRoute(null,
  "{controller}/{id}", // URL with parameters
  new { controller = "Home", action = "Get", id = UrlParameter.Optional },
  new { httpMethod = new HttpMethodConstraint("GET") }
);

routes.MapRoute(null,
  "{controller}/{id}", // URL with parameters
  new { controller = "Home", action = "Put", id = UrlParameter.Optional },
  new { httpMethod = new HttpMethodConstraint("PUT") }
);

routes.MapRoute(null,
  "{controller}", // URL with parameters
  new { controller = "Home", action = "Post", id = UrlParameter.Optional },
  new { httpMethod = new HttpMethodConstraint("POST") }
);

routes.MapRoute(null,
  "{controller}/{id}", // URL with parameters
  new { controller = "Home", action = "Delete", id = UrlParameter.Optional },
  new { httpMethod = new HttpMethodConstraint("DELETE") }
);

routes.MapRoute(
  "Default", // Route name
  "{controller}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

... these new 4 routes all have the same URL pattern, with the exception of the POST (since you should POST to the collection, yet PUT to the specific id). ...这些新的4条路由都有相同的URL模式,但POST除外(因为你应该POST到集合,但PUT到特定的id)。 However, the different HttpMethodConstraints tell MVC routing only to match the route when the httpMethod corresponds. 但是,不同的HttpMethodConstraints只告诉MVC路由以匹配httpMethod对应的路由。 So when someone sends DELETE to /MyItems/6, MVC will not match the first 3 routes, but will match the 4th. 因此,当有人将DELETE发送到/ MyItems / 6时,MVC将不匹配前3条路线,但会匹配第4条路线。 Similarly, if someone sends a PUT to /MyItems/13, MVC will not match the first 2 routes, but will match the 3rd. 类似地,如果有人向/ MyItems / 13发送PUT,MVC将不匹配前2个路由,但将匹配第3个路由。

Once MVC matches the route, it will use the default action for that route definition. 一旦MVC匹配路由,它将使用该路由定义的默认操作。 So when someone sends a DELETE, it will map to the Delete method on your controller. 因此,当有人发送DELETE时,它将映射到控制器上的Delete方法。

Consider using the AttributeRouting nuget package. 考虑使用AttributeRouting nuget包。 It has support for restful conventions . 它支持宁静的约定

If you're using MVC4 Beta, why not also use WebAPI? 如果您正在使用MVC4 Beta,为什么不使用WebAPI呢?

That aside, I don't think the routing engine groks the various HTTP verbs... so, you're kind of stuck. 除此之外,我不认为路由引擎会查找各种HTTP动词...所以,你有点卡住了。 Unless you overload a single Action method for all of them and do this: 除非为所有这些方法重载单个Action方法并执行此操作:

routes.MapRoute(
  "Default", // Route name
  "{controller}/{id}", // URL with parameters
  new { controller = "Home", action = "Restifier", id = UrlParameter.Optional }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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