I want to embed a link to a controller action in my page so I can use it from javascript. Something like
var pollAction = \'/Mycontroller/CheckStatus\'
If your page or control inherits from ViewPage
or ViewUserControl
, use the Url.Action
method.
If not, use this instead:
String url = RouteTable.Routes.GetVirtualPath
(
((MvcHandler) HttpContext.Current.CurrentHandler).RequestContext,
new RouteValueDictionary
(
new
{
controller = "MyController",
action = "CheckState",
id = idParameter
}
)
).VirtualPath;
Place this inside a method on your code-behind and call it from the HTML view.
Have you tried something along these lines?
var pollAction = '<%=Url.Action("CheckStatus", "MyController") %>';