2011-07-21 16 views

答えて

76

は、URLヘルパーのAction方法使用してのような意味:

<%= Url.Action("DoThis", "MyController") %> 

やカミソリで:

@Url.Action("DoThis", "MyController") 

あなたの相対URL(/MyController/DoThis)を得ます。

そして、あなたは絶対URL(http://localhost:8385/MyController/DoThis)取得したい場合:

<%= Url.Action("DoThis", "MyController", null, Request.Url.Scheme, null) %> 
1

をあなたはあなただけのコントローラの名前と、必要なアクションに渡すことができ、それが生成されますUrl.Actionメソッドを使用することができますあなたのための適切なURL

Url.Action("DoThis","MyController") 
+0

正しい構文は次のとおりです。 'アクション(文字列actionNameの、string controllerName) '、最初のパラメータはactionで、2番目のパラメータはコントローラ –

9

何日か前、私は(How to build absolute action URLs using the UrlHelper classを参照)は非常にトピックに関するブログ記事を書きました。 Darin Dimitrovが言及したように、パラメータが明示的に指定されている場合、UrlHelper.Actionは絶対URLを生成します。

しかし、私は読みやすさのためにカスタム拡張メソッドを記述することをお勧め:

/// <summary> 
/// Generates a fully qualified URL to an action method by using 
/// the specified action name, controller name and route values. 
/// </summary> 
/// <param name="url">The URL helper.</param> 
/// <param name="actionName">The name of the action method.</param> 
/// <param name="controllerName">The name of the controller.</param> 
/// <param name="routeValues">The route values.</param> 
/// <returns>The absolute URL.</returns> 
public static string AbsoluteAction(this UrlHelper url, 
    string actionName, string controllerName, object routeValues = null) 
{ 
    string scheme = url.RequestContext.HttpContext.Request.Url.Scheme; 

    return url.Action(actionName, controllerName, routeValues, scheme); 
} 

方法は、このように呼び出すことができます:@Url.AbsoluteAction("SomeAction", "SomeController")

関連する問題