asp.net - 在 Controller .NET MVC 中创建 URL

标签 asp.net asp.net-mvc

我需要能够在 Controller 上的操作中构建一个链接来发送电子邮件。执行此操作的最佳实践是什么?我不想自己构建它,以防我的路线发生变化。

我应该查看每封电子邮件并渲染并发送它吗?这可能是一个好方法。

最佳答案

如果您只想获取某个操作的路径,请使用UrlHelper:

UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
string url = u.Action("About", "Home", null);

如果您想创建超链接:

string link = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, System.Web.Routing.RouteTable.Routes, "My link", "Root", "About", "Home", null, null);

Intellisense 将为您提供每个参数的含义。

<小时/>

评论更新: Controller 已经有一个UrlHelper:

string url = this.Url.Action("About", "Home", null); 

关于asp.net - 在 Controller .NET MVC 中创建 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/699782/

相关文章:

javascript - 无法为 <label> 标记内的单选按钮注册更改处理程序

asp.net - 如何遵守 IIS6 中的 "Serve static content from a cookieless domain"页面速度规则?

asp.net-mvc - ASP.NET MVC 3,类似于 Razor 布局 Controller ?

c# - MVC5 区域不工作

c# - 使用 MVC 在 CQRS 中应该在哪里进行验证?

asp.net - 使 WCF Web 服务与 GET 请求一起工作

c# - 首先通过将类型作为参数传递来动态实例化 Entity Framework 数据库中的模型对象

c# - 使用 ASP.NET MVC 时如何设置新页面/ View 的 URL

asp.net-mvc - 是否可以从 asp.net mvc 应用程序和 python 应用程序共享 session 数据?

asp.net-mvc - 向表单提交添加附加参数