C# 无操作的ASP MVC路由 我想把这个动作放在URL中,因为我不认为这是一种休息的方法。默认路线应为: "{controller}/{id}"

C# 无操作的ASP MVC路由 我想把这个动作放在URL中,因为我不认为这是一种休息的方法。默认路线应为: "{controller}/{id}",c#,asp.net-mvc,C#,Asp.net Mvc,然后调用与所用HTTP方法对应的操作。例如,我正在装饰一个PUT动作,如下所示: [HttpPut] public ActionResult Change() { return View(); } 然而,当卷曲这个,我得到一个404。所以我做错了什么,以前有人尝试过这种方法吗 我正在使用MVC4测试版 这就是我为设置路线所做的全部工作: routes.MapRoute( name: "Default", url: "{controller}/{i

然后调用与所用HTTP方法对应的操作。例如,我正在装饰一个PUT动作,如下所示:

[HttpPut]
public ActionResult Change()
{
    return View();
}
然而,当卷曲这个,我得到一个404。所以我做错了什么,以前有人尝试过这种方法吗

我正在使用MVC4测试版

这就是我为设置路线所做的全部工作:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{id}",
        defaults: new { controller = "Home", action = "Index", id = RouteParameter.Optional }
    );

如果您使用的是MVC4测试版,为什么不同时使用WebAPI呢

除此之外,我不认为路由引擎会搜索各种HTTP动词。。。所以,你有点被卡住了。除非为所有操作重载单个操作方法并执行此操作:

routes.MapRoute(
  "Default", // Route name
  "{controller}/{id}", // URL with parameters
  new { controller = "Home", action = "Restifier", id = UrlParameter.Optional }
);
MVC中的action method selector最多只允许对同一命名方法有2个action method重载。我理解你是从哪里来的,你只想用{controller}/{id}作为URL路径,但是你可能用了错误的方法

如果您的控制器只有2个操作方法,比如1个用于GET,1个用于PUT,那么您可以只命名这两个操作索引,就像我上面所做的那样,或者像这样:

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

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

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

[HttpDelete]
public ActionResult Delete()
{
    return View();
}
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 }
);
如果控制器上有两个以上的方法,则可以为其他操作创建新的自定义路由。您的控制器可以如下所示:

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

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

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

[HttpDelete]
public ActionResult Delete()
{
    return View();
}
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 }
);
。。。如果您的global.asax如下所示:

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

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

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

[HttpDelete]
public ActionResult Delete()
{
    return View();
}
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 }
);
。。。这些新的4条路由都具有相同的URL模式,但POST除外(因为您应该发布到集合中,但要放到特定的id中)。但是,不同的HttpMethodConstraints告诉MVC routing仅在httpMethod对应时匹配路由。因此,当有人向/MyItems/6发送DELETE时,MVC将不匹配前3条路由,而是匹配第4条路由。类似地,如果有人向/MyItems/13发送PUT,MVC将不匹配前2条路由,而是匹配第3条路由


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

考虑使用nuget包。它支持。

您没有显示您实际如何配置路由的代码,因此很难说您是否做错了什么。谢谢Alexei,我添加了路由。这很有意义-您从@danlundwig获得了答案-您的操作名称“Index”在控制器中找不到。谢谢,这很不幸,但如果这就是需要的,那么我想这就是需要的。我不使用WebAPI,因为它认为网页和API应该有不同的URL,我不同意这一点。URL应该指向资源,而不是如何使用资源。这就是标题的作用。看一看,它可能会更自然地做你想做的事情。令人惊讶的是,这确实有效。我将尝试dotjoe的方法,但这是一个可行的退路。