我正在向混合WebForms/MVC站点添加一些UI功能.在这种情况下,我将一些AJAX UI功能添加到WebForms页面(通过jQuery),数据来自MVC JsonResult.一切都在100%工作,但有一个例外:
我想实现AntiForgeryToken的XSRF保护.我已经将它与我的纯MVC应用程序上的ValidateAntiForgeryToken属性结合使用,但是想知道如何在WebForms中实现Html.AntiForgeryToken()方法. 以下是使用UrlHelper的示例.
我正确地将ViewContext/RequestContext"模拟"起来有些麻烦.我应该如何在WebForms页面中使用HtmlHelpers?
编辑:我想从我的WebForms页面检索AntiForgeryToken,而不是从MVC JsonResult.
我想从一个无法访问ViewContext的实用程序类创建一个路由.
这可能吗?似乎没有任何相当的ViewContext.Current
我试过在Routing和HttpContext的所有构造函数中钓鱼但是不能完全达到我想要的.
这就是我正在寻找的 - 虽然这不起作用,因为RouteTable.Routes它是类型RouteCollection而不是RouteData.如此接近 - 但到目前为止:-)
RequestContext requestContext = new RequestContext(HttpContext.Current, RouteTable.Routes);
UrlHelper url = new UrlHelper(requestContext);
var urlString = url.RouteUrl(new {controller="DynamicImage", action="Button", text="Hello World"});
Run Code Online (Sandbox Code Playgroud)
注意:RequestContest的类型为System.Web.Routing.RequestContext,而不是HttpContext