ASP。NET MVC -如何获得操作的完整路径

时间:2022-11-30 22:42:51

Inside of a View can I get a full route information to an action?

在视图的内部,我能得到一个动作的完整路径信息吗?

If I have an action called DoThis in a controller MyController. Can I get to a path of "/MyController/DoThis/"?

如果我在控制器MyController中有一个叫DoThis的动作。我能找到路径"/MyController/DoThis/"吗?

3 个解决方案

#1


76  

You mean like using the Action method on the Url helper:

你的意思是像在Url助手上使用操作方法:

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

or in Razor:

或者在剃刀:

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

which will give you a relative url (/MyController/DoThis).

它会给你一个相对url (/MyController/DoThis)。

And if you wanted to get an absolute url (http://localhost:8385/MyController/DoThis):

如果您想获得一个绝对url (http://localhost:8385/MyController/DoThis):

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

#2


9  

Some days ago, I wrote a blog post about that very topic (see How to build absolute action URLs using the UrlHelper class). As Darin Dimitrov mentioned: UrlHelper.Action will generate absolute URLs if the protocol parameter is specified explicitly.

前几天,我写了一篇关于这个主题的博文(参见如何使用UrlHelper类构建绝对的操作url)。正如Darin Dimitrov提到的:UrlHelper。如果显式地指定了协议参数,操作将生成绝对url。

However, I suggest to write a custom extension method for the sake of readability:

但是,为了便于阅读,我建议编写一个自定义扩展方法:

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

The method can then be called like this: @Url.AbsoluteAction("SomeAction", "SomeController")

然后可以这样调用该方法:@Url。AbsoluteAction(“SomeAction”、“SomeController”)

#3


1  

You can use the Url.Action method where you can just pass in the name of the controller and the action you want and it will generate the appropriate URL for you e.g.

您可以使用Url。Action方法,你只需输入控制器的名称和你想要的动作,它就会为你生成相应的URL。

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

#1


76  

You mean like using the Action method on the Url helper:

你的意思是像在Url助手上使用操作方法:

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

or in Razor:

或者在剃刀:

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

which will give you a relative url (/MyController/DoThis).

它会给你一个相对url (/MyController/DoThis)。

And if you wanted to get an absolute url (http://localhost:8385/MyController/DoThis):

如果您想获得一个绝对url (http://localhost:8385/MyController/DoThis):

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

#2


9  

Some days ago, I wrote a blog post about that very topic (see How to build absolute action URLs using the UrlHelper class). As Darin Dimitrov mentioned: UrlHelper.Action will generate absolute URLs if the protocol parameter is specified explicitly.

前几天,我写了一篇关于这个主题的博文(参见如何使用UrlHelper类构建绝对的操作url)。正如Darin Dimitrov提到的:UrlHelper。如果显式地指定了协议参数,操作将生成绝对url。

However, I suggest to write a custom extension method for the sake of readability:

但是,为了便于阅读,我建议编写一个自定义扩展方法:

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

The method can then be called like this: @Url.AbsoluteAction("SomeAction", "SomeController")

然后可以这样调用该方法:@Url。AbsoluteAction(“SomeAction”、“SomeController”)

#3


1  

You can use the Url.Action method where you can just pass in the name of the controller and the action you want and it will generate the appropriate URL for you e.g.

您可以使用Url。Action方法,你只需输入控制器的名称和你想要的动作,它就会为你生成相应的URL。

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