c# - 将路由传递给 Controller ​​操作

标签 c# asp.net asp.net-mvc

我想将路由传递给 Controller ​​操作。例如,如果我有一个 Controller 操作 /Files/Index,我想将路由传递给 Controller ​​,例如 /Files/some/path/here 这样我的Index 操作选择 /some/path/here。这可能吗?

public class FilesController : Controller
{
    public HttpResponseMessage Index(string route)
    {
        // `route` should contain everything 
        // after the controller action
    }
}

最佳答案

最直接的方法是使用像这样的查询字符串

http://www.website.com/Home/Index?route=%2fFiles%2fsome%2fpath%2fhere

或作为操作链接中的参数

@Html.ActionLink("Go To Index", "Index", "Home", new {route= HttpUtility.HtmlEncode(/Files/some/path/here)}, null)

确保像上面那样转义/字符

关于c# - 将路由传递给 Controller ​​操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876578/

相关文章:

c# - 在 WPF 中通过 XAML 代码加载外部图像?

javascript - 通过 jQuery GET 调用传递参数在服务器端接收 null

asp.net-mvc - ASP.NET MVC + ORM

c# - 使用 C# 模拟 TCP/IP 连接

c# - 多维数组,向量访问的元素

c# - Libgit2Sharp:获取两个标签之间的文件列表

asp.net - 将 MVC-3 升级到 MVC-4

c# - 是否可以对查询参数使用 Web API 模型验证?

c# - 如何设置动态创建的表行的属性?

ASP.NET MVP 与 ASP.NET MVC