views:

116

answers:

1

Hi folks,

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 :)

+3  A: 

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

<%=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.

<%=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 :)

Cipher
Are there any other strongly-typed solutions?
Pure.Krome
No there no other strongly-typed solutions included with MVC or Futures, however David Ebbo does have a really good implimentation. [http://blogs.msdn.com/davidebb/archive/2009/06/01/a-buildprovider-to-simplify-your-asp-net-mvc-action-links.aspx] Also be sure to look at his latest post which uses T4 templates. Hope this helps!
Cipher