2011-07-21 19 views

cevap

76

Sen URL yardımcı üzerinde Action yöntemi kullanarak gibi demek:

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

veya Razor içinde:

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

size göreceli bir URL ( /MyController/DoThis) verecektir. Mutlak bir URL ( http://localhost:8385/MyController/DoThis) almak istiyorsa

Ve: sadece istediğiniz denetleyicisi ve eylem adına geçebilir nerede

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

Sen Url.Action yöntemi kullanabilirsiniz ve onu üretecektir sizin için uygun URL

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

doğru sözdizimi şöyledir:

Ancak, okunabilirliği uğruna özel bir uzantı yöntem yazmak önermek 'Eylem (dize ActionName , string controllerName), ilk param eylem olmalı ve ikincisi denetleyici –

9

Bazı gün önce, (How to build absolute action URLs using the UrlHelper class bakınız) çok konu hakkında bir blog yazısı yazdı. Darin Dimitrov'un bahsettiği gibi: parametresi açıkça belirtilmişse UrlHelper.Action mutlak URL'ler üretecektir.

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

yöntemi sonra böyle çağrılabilir:: @Url.AbsoluteAction("SomeAction", "SomeController")