2011-08-16 30 views

Trả lời

0

Lớp helper:

public static class UrlHelper 
    { 
    private static System.Web.Mvc.UrlHelper _urlHelper; 

    public static System.Web.Mvc.UrlHelper GetFromContext() 
    { 
     if (_urlHelper == null) 
     { 
     if (HttpContext.Current == null) 
     { 
     throw new HttpException("Current httpcontext is null!"); 
     } 

     if (!(HttpContext.Current.CurrentHandler is System.Web.Mvc.MvcHandler)) 
     { 
      throw new HttpException("Type casting is failed!"); 
     } 

     _urlHelper = new System.Web.Mvc.UrlHelper(((System.Web.Mvc.MvcHandler)HttpContext.Current.CurrentHandler).RequestContext); 
     } 

     return _urlHelper; 
    } 
    } 

Việc kêu gọi:

UrlHelper.GetFromContext().Action("action", "controller"); 
13

giả mã của bạn đang chạy trong bối cảnh của một yêu cầu http, bạn có thể làm như sau từ một phương pháp tĩnh:

new UrlHelper(HttpContext.Current.Request.RequestContext); 
+0

Đây phải là câu trả lời. – ivowiblo

Các vấn đề liên quan