ASP.NET MVC public alternative to UrlHelper.GenerateUrl

后端 未结 2 713
深忆病人
深忆病人 2020-12-09 04:41

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\'


        
相关标签:
2条回答
  • 2020-12-09 05:20

    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.

    0 讨论(0)
  • 2020-12-09 05:29

    Have you tried something along these lines?

    var pollAction = '<%=Url.Action("CheckStatus", "MyController") %>';
    
    0 讨论(0)
提交回复
热议问题