简体   繁体   English

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

[英]How do I set up this route with ASP.NET MVC?

I'm trying to create a route and then programatically retrieve the url of that route (so i can pass it to my jquery-rater.js code). 我正在尝试创建一条路线,然后以编程方式检索该路线的url(以便可以将其传递给我的jquery-rater.js代码)。

So, i wish to have the following url: /vote/create The user will need to HTTP-POST to it. 因此,我希望具有以下URL:/ vote / create用户将需要HTTP-POST到它。 Posting the two values: 1. PostId 2. Vote Score (byte from 1<->5). 发布两个值:1. PostId 2.投票分数(从1 <-> 5开始的字节)。

This is my route info: 这是我的路线信息:

routes.MapRoute(
    "Vote-Create",
    "vote/create/",
    new {controller = "Post", action = "VoteCreate"}
);

This is my action method (which I'm also not too sure if it's right). 这是我的操作方法(我也不太确定是否正确)。

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult VoteCreate(int postId, byte score)
{ .. }

Finally, this is where I need to determine the uri (and i'm not sure how) :- 最后,这是我需要确定uri的地方(而且我不确定如何):-

<script type="text/javascript">$(function() 
     { $('#rating<%= Model.Post.PostId %>')
           .rater({ postHref: 'URI IN HERE' }); });
</script>

At first, I thought I might use the <%= Html.BuildUrlFromExpression(..) %> but i'm not sure how. 起初,我以为我可能会使用<%= Html.BuildUrlFromExpression(..)%>,但我不确定如何使用。

Is there a better / proper way? 有没有更好/合适的方法?

Thanks folks :) 谢谢大家:)

There is a UrlHelper on the ViewPage that has plunty of goodies to do this. ViewPage上有一个UrlHelper,它具有很多功能。 Since you are naming the route ("View-Create") you can do something like this 由于您要命名路线(“查看创建”),因此您可以执行以下操作

<%=Url.RouteUrl("Vote-Create") %>

in your code. 在您的代码中。 If you dont want to use your the route name you can also use the Action method, passing in the action name and whatever values you need. 如果您不想使用路由名称,则也可以使用Action方法,传入操作名称和所需的任何值。

<%=Url.Action("VoteCreate") %>

Here is the url helper reference 这是网址帮助器参考

Edit: Your Route looks correct as is, and when testing this on my box it works like a charm :) 编辑:您的路线看起来像是正确的,并且在我的盒子上测试时,它就像一个魅力:)

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

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