ASP MVC路线不行动 [英] ASP MVC Routes without actions

查看:81
本文介绍了ASP MVC路线不行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想离开了行动,网址,因为我不认为这是一个宁静的做法。默认路由应该是:

 {}控制器/(编号)

然后调用对应于所使用的HTTP方法的操作。比如我装饰PUT动作像这样的:

  [HttpPut]
公众的ActionResult变化()
{
    返回查看();
}

不过卷曲ING这样的时候,我得到一个404。所以我做错了什么,任何一个尝试过这种做法?

我使用的是MVC4测试版。

编辑:这是我做的组建路线

  routes.MapRoute(
        名称:默认,
        网址:{}控制器/(编号),
        默认:新{控制器=家,行动=索引,ID = RouteParameter.Optional}
    );


解决方案

  [HttpPut]
[ActionName(指数)]
公众的ActionResult变化()
{
    返回查看();
}

MVC中的操作方法选择将只允许你有最多2操作方法重载的同名方法。我明白了,你是从想只有{控制器} / {ID}的URL路径来了,但你可能在错误的方式去它。

如果你只有2控制器的操作方法,说1 GET和1 PUT,那么你可以既命名你的行为指数,要么像我一样上面,或者是这样的:

  [HttpPut]
公众的ActionResult指数()
{
    返回查看();
}

如果您在控制器上超过2种方法,你可以创建其他的操作新的自定义路线。您的控制器能是这样的:

  [HttpPut]
公众的ActionResult认沽()
{
    返回查看();
}[HttpPost]
公众的ActionResult邮政()
{
    返回查看();
}[HTTPGET]
公众的ActionResult的get()
{
    返回查看();
}[HttpDelete]
公众的ActionResult删除()
{
    返回查看();
}

...如果您的Global.asax是这样的:

  routes.MapRoute(NULL,
  {}控制器/(编号),// URL带参数
  新{控制器=家,行动=获取,ID = UrlParameter.Optional},
  新{列举HTTPMethod =新HttpMethodConstraint(GET)}
);routes.MapRoute(NULL,
  {}控制器/(编号),// URL带参数
  新{控制器=家,行动=放,ID = UrlParameter.Optional},
  新{列举HTTPMethod =新HttpMethodConstraint(PUT)}
);routes.MapRoute(NULL,
  {}控制器,//带参数的URL
  新{控制器=家,行动=邮报,ID = UrlParameter.Optional},
  新{列举HTTPMethod =新HttpMethodConstraint(POST)}
);routes.MapRoute(NULL,
  {}控制器/(编号),// URL带参数
  新{控制器=家,行动=删除,ID = UrlParameter.Optional},
  新{列举HTTPMethod =新HttpMethodConstraint(删除)}
);routes.MapRoute(
  默认,//路线名称
  {}控制器/(编号),// URL带参数
  新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);

......这些新的4路都具有相同的URL模式,用POST之外(因为你应该张贴到集合,但投入到具体的ID)。然而,不同HttpMethodConstraints只告诉MVC路由时列举HTTPMethod对应于匹配的路线。所以,当有人向DELETE键/ MyItems / 6,MVC将不匹配的第3路线,但将匹配4。同样,如果有人发送一个PUT到/ MyItems / 13,MVC将不匹配的第一个2路由,但将匹配3。

在MVC的路由匹配,就会使用该路由定义的默认操作。因此,当有人发送DELETE,它会映射到控制器上的Delete方法。

I would like to leave out the action in url as I do not consider that to be a restful approach. Default routes should be:

"{controller}/{id}"

And then call the action that corresponds to the HTTP method used. For example I am decorating a PUT action like thus:

[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?

I'm using the MVC4 beta.

EDIT: 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. 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.

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:

[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:

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). However, the different HttpMethodConstraints tell MVC routing only to match the route when the httpMethod corresponds. So when someone sends DELETE to /MyItems/6, MVC will not match the first 3 routes, but will match the 4th. Similarly, if someone sends a PUT to /MyItems/13, MVC will not match the first 2 routes, but will match the 3rd.

Once MVC matches the route, it will use the default action for that route definition. So when someone sends a DELETE, it will map to the Delete method on your controller.

这篇关于ASP MVC路线不行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆