如何使用 ASP.NET MVC 路由设置 RESTFul URL?

发布于 2024-10-20 17:40:46 字数 315 浏览 2 评论 0原文

我有以下 URL:

/controller/action/value

和此操作:

public ActionResult Get(string configName,string addParams)
{
}

How do I set up my Routing Table to get the Routing Engine Bind the value to the configName 参数Config 控制器中有任何操作吗?

I have this URL:

/controller/action/value

and this action:

public ActionResult Get(string configName,string addParams)
{
}

How do I set up my routing table to get the routing engine bind the value to the configName parameter for any action in the Config controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-10-27 17:40:46

嗯,首先,这是不完整的。您没有方法名称。

其次,这已经适用于以下格式的 URL:

/controller/action?configName=foo&addparams=bar

以下是如何使用漂亮的路由来做到这一点:

routes.MapRoute(
      "YourMapping",                                            
      "{controller}/{action}/{configName}/{addParams}");

或者

routes.MapRoute(
      "YourMapping",                                            
      "{controller}/{configName}/{addParams}",     
      new {
          controller = "YourController",
          action = "YourAction"
      },
      new {
          controller = "YourController"  // Constraint
      });

如果您想从 URL 中排除操作。

Well, first off, that is incomplete. You don't have a method name.

Secondly, this will already work with URLs of the format:

/controller/action?configName=foo&addparams=bar

Here's how to do it with pretty routes:

routes.MapRoute(
      "YourMapping",                                            
      "{controller}/{action}/{configName}/{addParams}");

or

routes.MapRoute(
      "YourMapping",                                            
      "{controller}/{configName}/{addParams}",     
      new {
          controller = "YourController",
          action = "YourAction"
      },
      new {
          controller = "YourController"  // Constraint
      });

if you want to exclude the action from the URL.

孤城病女 2024-10-27 17:40:46

您可以在默认路由之上添加新路由,

routes.MapRoute(
  "Config", 
  "config/{action}/{configName}/{addParams}",
  new { controller = "Config", action = "Index" }
);

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

这将允许您使用路由 /config/actionName/configName/addParamsValue。您的其他路线应该不会受到此影响。

You could add a new route above the default

routes.MapRoute(
  "Config", 
  "config/{action}/{configName}/{addParams}",
  new { controller = "Config", action = "Index" }
);

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

Which will allow you to use the route /config/actionName/configName/addParamsValue. Your other routes should be unaffected by this.

淡淡離愁欲言轉身 2024-10-27 17:40:46
routes.MapRoute(
      "ValueMapping",                                            
      "config/{action}/{configName}/{addParams}",          
      new { controller = "Config", action = "Index", configName= UrlParameter.Optional, addParams = UrlParameter.Optional }  // Parameter defaults);

将默认控制器设置为 Home,默认操作为 Index,

因此 URL:
/config/get/configNameValue/AddParamValue

将匹配此方法:

public ActionResult Get(string configName,string addParams)
{
//Do Stuff 
}
routes.MapRoute(
      "ValueMapping",                                            
      "config/{action}/{configName}/{addParams}",          
      new { controller = "Config", action = "Index", configName= UrlParameter.Optional, addParams = UrlParameter.Optional }  // Parameter defaults);

Setting default Controller to Home, with a Default Action of Index

So the Url:
/config/get/configNameValue/AddParamValue

would match this Method:

public ActionResult Get(string configName,string addParams)
{
//Do Stuff 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文