如何使用Asp MVC Url Helpers生成RESTful链接?(How do I use Asp MVC Url Helpers to generate RESTful links?)

编程入门 行业动态 更新时间:2024-10-26 15:16:46
如何使用Asp MVC Url Helpers生成RESTful链接?(How do I use Asp MVC Url Helpers to generate RESTful links?)

我正在尝试使用Html.ActionLink以这种形式生成链接:

/动作/型号/ ID /参数1 /参数2

我用过: <%= Html.ActionLink("Link Text", "Action", "Model", new { id = var, parament1=var1 }, null) %>但它总是看起来像/ Action /型号/编号?参数1 =可变

我在Stackoverflow和其他地方看到了类似的问题,但我找不到一个有效/有意义的解决方案。 任何帮助,将不胜感激。

@John:我注册了以下路线: routes.MapRoute("Alternate","{controller}/{action}/{id}/{Heading}", new { controller = "Designation", action = "Details", id = "", Heading = "" }

然后我在我的视图中有以下代码: <%= Html.ActionLink("Link Text", "Action", "Model", new { Id = model.Id, Heading = model.Heading }, null) %>

我得到/ Model / Action / Id?标题= var1我希望它为/ Model / Action / Id / var1

解答:我在global.asax.cs文件中添加了以下路由,并正确生成了链接。 routes.MapRoute(“Something”,“MyModel / MyAction / {Id} / {Heading}”,new {controller =“MyModel”,action =“MyAction”,id =“”,heading =“”},new {controller = @“[^。] *”});

I'm trying to use Html.ActionLink to generate a link with in this form:

/Action/Model/Id/Parameter1/Parameter2

I've used: <%= Html.ActionLink("Link Text", "Action", "Model", new { id = var, parament1=var1 }, null) %> but it always ends up looking like /Action/Model/Id?parameter1=variable

I've seen similar questions on Stackoverflow and elsewhere, but I can't find a solution that works/makes sense. Any help would be appreciated.

@John: I have the following route registered: routes.MapRoute("Alternate","{controller}/{action}/{id}/{Heading}", new { controller = "Designation", action = "Details", id = "", Heading = "" }

I then have the following code in my view: <%= Html.ActionLink("Link Text", "Action", "Model", new { Id = model.Id, Heading = model.Heading }, null) %>

I get /Model/Action/Id?Heading=var1 I want it as /Model/Action/Id/var1

ANSWER: I added the following route to my global.asax.cs file and the links generated correctly. routes.MapRoute("Something", "MyModel/MyAction/{Id}/{Heading}", new { controller = "MyModel", action = "MyAction", id = "", heading = "" }, new { controller = @"[^.]*" });

最满意答案

这是一篇非常好的文章,你应该阅读http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

Here is an excelent article you should read http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

更多推荐

本文发布于:2023-07-26 19:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1280260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   链接   MVC   Asp   Helpers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!