ASP.NET MVC: How to create a usable UrlHelper instance?

前端 未结 4 763
Happy的楠姐
Happy的楠姐 2021-02-13 05:26

I am using quartz.net to schedule regular events within asp.net mvc application.

The scheduled job should call a service layer script that requires a UrlHelper instance

4条回答
  •  没有蜡笔的小新
    2021-02-13 05:54

    You need a RequestContext to create a UrlHelper. In one of my HtmlHelper extension methods, I do it like this:

    public static string ScriptUrl(this HtmlHelper html, string script)
    {
        var url = new UrlHelper(html.ViewContext.RequestContext);
        ...
    }
    

    How you get the RequestContext is dependent on your application.

提交回复
热议问题