以编程方式获取 ASP.NET MVC 应用程序的控制器地址

发布于 2024-09-24 14:51:51 字数 284 浏览 4 评论 0原文

假设我想从 MVC 应用程序发送一封电子邮件,其中包含返回某个项目的链接。

所以我想在我的控制器中添加一些东西,例如:

string link = "www.mysite/mycontroller/itemdetails/23"; 

我可以以编程方式检索它,以便它可以在我发生的任何服务器/配置上工作吗?

例如

string link = GetCurrentActionRoute() + "23";

Say I want to send an email from my MVC application containing a link back to an item.

So I want something in my controller like:

string link = "www.mysite/mycontroller/itemdetails/23"; 

can I retrieve this programatically so it works on whatever server/configuration i happen stick it on?

eg

string link = GetCurrentActionRoute() + "23";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以酷 2024-10-01 14:51:51
public ActionResult Index()
{
    string link = Url.Action(
        (string)RouteData.Values["action"],
        (string)RouteData.Values["controller"], 
        new { id = "23" }, 
        Request.Url.Scheme
    );
    // TODO: do something with the generated link
    return View();
}
public ActionResult Index()
{
    string link = Url.Action(
        (string)RouteData.Values["action"],
        (string)RouteData.Values["controller"], 
        new { id = "23" }, 
        Request.Url.Scheme
    );
    // TODO: do something with the generated link
    return View();
}
苯莒 2024-10-01 14:51:51

我使用控制器类中的以下扩展方法来生成各种操作的链接。

示例:

string link = this.Action("Play");
string fullLink = this.FullUrlAction("Play", new { id = 1 });

方法:

/// <summary>
/// Helper Extensions to the Controller class.
/// </summary>
public static class ControllerExtensions
{
    /// <summary>
    /// Get an action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="action">Action to provide the link for.</param>
    /// <returns>Link Url for the Action.</returns>
    public static string Action(this Controller controller, string action) 
    {
        RouteValueDictionary rvd = new RouteValueDictionary
                                       {
                                           { "controller", controller.GetType().Name }, 
                                           { "action", action }
                                       };

        return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
    }

    /// <summary>
    /// Get an action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <returns>Link Url for the action with parameters.</returns>
    public static string Action(this Controller controller, string action, object parameters)
    {
        string controllerName = controller.GetType().Name;
        if (controllerName.EndsWith("Controller"))
        {
            controllerName = controllerName.Substring(0, controllerName.Length - 10);
        }

        return controller.Action(controllerName, action, parameters);
    }

    /// <summary>
    /// Get an action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="targetController">Target controller for action.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <returns>Link Url for the action on the given controller.</returns>
    public static string Action(this Controller controller, string targetController, string action, object parameters)
    {
        RouteValueDictionary rvd = new RouteValueDictionary(parameters)
                                       {
                                           { "controller", targetController }, 
                                           { "action", action }
                                       };

        return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
    }

    /// <summary>
    /// Get a fully qualified action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <param name="requestUrl">Current request URI.</param>
    /// <returns>Fully qualified Link Url.</returns>
    public static string FullUrlAction(this Controller controller, string action, object parameters, Uri requestUrl)
    {
        string controllerName = controller.GetType().Name;
        if (controllerName.EndsWith("Controller"))
        {
            controllerName = controllerName.Substring(0, controllerName.Length - 10);
        }

        return controller.FullUrlAction(controllerName, action, parameters, requestUrl);
    }

    /// <summary>
    /// Get a fully qualified action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="targetController">Target controller for action.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <param name="requestUrl">Current request URI.</param>
    /// <returns>Fully Qualified Link Url.</returns>
    public static string FullUrlAction(this Controller controller, string targetController, string action, object parameters, Uri requestUrl)
    {
        string testUrl = VirtualPathUtility.ToAbsolute(controller.Action(targetController, action, parameters));
        return new Uri(requestUrl, testUrl).ToString();
    }
}

I use the extension methods below from my Controller classes to generate links to various actions.

Examples:

string link = this.Action("Play");
string fullLink = this.FullUrlAction("Play", new { id = 1 });

Methods:

/// <summary>
/// Helper Extensions to the Controller class.
/// </summary>
public static class ControllerExtensions
{
    /// <summary>
    /// Get an action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="action">Action to provide the link for.</param>
    /// <returns>Link Url for the Action.</returns>
    public static string Action(this Controller controller, string action) 
    {
        RouteValueDictionary rvd = new RouteValueDictionary
                                       {
                                           { "controller", controller.GetType().Name }, 
                                           { "action", action }
                                       };

        return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
    }

    /// <summary>
    /// Get an action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <returns>Link Url for the action with parameters.</returns>
    public static string Action(this Controller controller, string action, object parameters)
    {
        string controllerName = controller.GetType().Name;
        if (controllerName.EndsWith("Controller"))
        {
            controllerName = controllerName.Substring(0, controllerName.Length - 10);
        }

        return controller.Action(controllerName, action, parameters);
    }

    /// <summary>
    /// Get an action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="targetController">Target controller for action.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <returns>Link Url for the action on the given controller.</returns>
    public static string Action(this Controller controller, string targetController, string action, object parameters)
    {
        RouteValueDictionary rvd = new RouteValueDictionary(parameters)
                                       {
                                           { "controller", targetController }, 
                                           { "action", action }
                                       };

        return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
    }

    /// <summary>
    /// Get a fully qualified action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <param name="requestUrl">Current request URI.</param>
    /// <returns>Fully qualified Link Url.</returns>
    public static string FullUrlAction(this Controller controller, string action, object parameters, Uri requestUrl)
    {
        string controllerName = controller.GetType().Name;
        if (controllerName.EndsWith("Controller"))
        {
            controllerName = controllerName.Substring(0, controllerName.Length - 10);
        }

        return controller.FullUrlAction(controllerName, action, parameters, requestUrl);
    }

    /// <summary>
    /// Get a fully qualified action link for a controller.
    /// </summary>
    /// <param name="controller">Controller instance.</param>
    /// <param name="targetController">Target controller for action.</param>
    /// <param name="action">Action Name.</param>
    /// <param name="parameters">Action Parameters.</param>
    /// <param name="requestUrl">Current request URI.</param>
    /// <returns>Fully Qualified Link Url.</returns>
    public static string FullUrlAction(this Controller controller, string targetController, string action, object parameters, Uri requestUrl)
    {
        string testUrl = VirtualPathUtility.ToAbsolute(controller.Action(targetController, action, parameters));
        return new Uri(requestUrl, testUrl).ToString();
    }
}
跨年 2024-10-01 14:51:51

如果链接指向与应用程序相同的位置,您可以考虑使用 Url.Action(...) 及其重载。

例如。

string actionRoute = Url.Action("action method name","controller name");

如果你想获得完整的路径,你可以使用 :

string domainRoute = HttpContext.Request.Url.AbsolutePath;

,然后将 id 与它连接起来。

If the link will point to the same location as that of the application ,you may consider using the Url.Action(...) and its overloads.

Eg.

string actionRoute = Url.Action("action method name","controller name");

If you want to get the complete path, you can use :

string domainRoute = HttpContext.Request.Url.AbsolutePath;

and then concatenate the id with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文