2011-08-16 9 views
9

ASP.NET MVCアプリケーションでRouteTableに「Asset」という名前のルートが追加され、静的メソッドがあります。ASP.NET MVCの静的メソッドのURLを取得する

この静的メソッドは、アプリケーションの「資産」ルートへのURLを生成できる必要があります。

どうすればいいですか?

答えて

0

ヘルパークラス:

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; 
    } 
    } 

呼び出し:

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

あなたのコードを仮定して、HTTPリクエストのコンテキストで実行されている、あなたは、静的メソッドから次の操作を実行できます。

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

これが答えになるはずです。 – ivowiblo

関連する問題