不要在 URL 中显示操作 - 路由
我有一个带有 Index() ViewResult 的 DirectoryController,它在视图中显示以下链接:
@Html.ActionLink("My file 1", "Index", "File", new { @id = "123" }, null) <br />
@Html.ActionLink("My file 2", "Index", "File", new { @id = "456" }, null) <br />
@Html.ActionLink("My file 3", "Index", "File", new { @id = "789" }, null) <br />
如果我单击其中一个链接,我最终会在 URL 中看到(例如)以下内容:
我想要什么真正喜欢的是 URL 中的以下内容:
http://www.domain.com/File/my-file-1< /a>
基本上,我希望在 URL 中看到控制器名称和自定义名称(例如文件名),而不显示实际的操作或id。
无论我点击哪个链接,它们都应该继续指向 FileController 的 Index() 方法,并传递适当的id。
我该怎么办呢?
我应该如何创建以及创建什么样的自定义路由?
谢谢
I have a DirectoryController with an Index() ViewResult which displays the following links in the view:
@Html.ActionLink("My file 1", "Index", "File", new { @id = "123" }, null) <br />
@Html.ActionLink("My file 2", "Index", "File", new { @id = "456" }, null) <br />
@Html.ActionLink("My file 3", "Index", "File", new { @id = "789" }, null) <br />
If I click on one of the links, I’ll end up having (for example) the following in the URL:
What I’d really like to have is the following in the URL:
Basically, I’d like to see the Controller name and a custom name (the name of the file for example) in the URL without showing the actual action or the id.
Regardless of the link I click, all of them should continue to point to the Index() method of the FileController passing along the appropriate id.
How would I go about this?
How and what kind of custom route should I create?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般概述:
您想要做的是创建路由映射,
例如翻译用户友好的 url,
您需要在您的控制器/global.asax 中进行一些更新
的路线示例您将拥有类似的内容
您的操作中
As general overview:
what you want to do is create route mapping
eg translating url that is user friendly
you would need to do some updates in your controller/ global.asax
example of route
in your action you will have something like
如果您将在 IIS 而不是 Casini 上运行此程序,则可以使用 URL 重写来实现所需的结果。
In case you'll be running this on IIS, rather than Casini, you can use URL re-writes to achieve the desired result.