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).
So, i wish to have the following url: /vote/create
The user will need to HTTP-POST to it. Posting the two values:
1. PostId
2. Vote Score (byte from 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) :-
<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.
Is there a better / proper way?
Thanks folks 🙂
There is a UrlHelper on the ViewPage that has plunty of goodies to do this.
Since you are naming the route (“View-Create”) you can do something like this
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.
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 🙂