C# 将路由传递给控制器操作
我想传递到控制器操作的路由。例如,如果我有一个控制器操作C# 将路由传递给控制器操作,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我想传递到控制器操作的路由。例如,如果我有一个控制器操作/Files/Index,我想传递一个到控制器的路由,比如/Files/some/path/here,这样我的Index操作将拾取/some/path/here。这可能吗 public class FilesController : Controller { public HttpResponseMessage Index(string route) { // `route` should contain e
/Files/Index
,我想传递一个到控制器的路由,比如/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
}
}
对。指定自定义管线 MVC4 MVC5
对。指定自定义管线 MVC4 MVC5
最简单的方法是使用查询字符串,如
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)
确保转义上面的
/
字符最简单的方法是使用如下查询字符串
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)
确保转义上面的
/
字符您需要做的不仅仅是定义路由
在RouteConfig.cs中:
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.MapRoute(
name: "FilePathRoute",
url: "FilePath/Index/{*filePath}",
defaults: new{ controller="FilePath", action="Index"});
此外,您必须告诉web服务器不要截取看起来像静态文件的URL。例如,在IIS Express中,清除前提条件
属性:
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule"
preCondition="" />
视图/FilePath/Index.cshtml:
@{
布局=空;
}
@文件路径
感谢亚当·弗里曼,ISBN 1-4302-4236-1;第13章,“URL路由”,标题“磁盘文件路由请求”。用那本书构造这个答案花了大约15分钟。你需要做的不仅仅是定义路线 在RouteConfig.cs中:
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.MapRoute(
name: "FilePathRoute",
url: "FilePath/Index/{*filePath}",
defaults: new{ controller="FilePath", action="Index"});
此外,您必须告诉web服务器不要截取看起来像静态文件的URL。例如,在IIS Express中,清除前提条件
属性:
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule"
preCondition="" />
视图/FilePath/Index.cshtml:
@{
布局=空;
}
@文件路径
感谢亚当·弗里曼,ISBN 1-4302-4236-1;第13章,“URL路由”,标题“磁盘文件路由请求”。使用那本书构造这个答案花了大约15分钟。当我尝试
/Files/Index/some/path/here
时,它显示“404-未找到文件或目录”您使用的是路由属性还是MVC4技术?您注册了MVC5路由吗?非常好,它适用于路由。知道如何允许文件扩展名吗/Files/test/path/
有效,但/Files/test/path/somefile.pdf
无效。我注意到它适用于长路径,但对于像文件/p/test.pdf
这样的短路径,它是404。它显示“404-找不到文件或目录”当我尝试/Files/Index/some/path/here
时,您使用的是路由属性还是MVC4技术?您是否注册了MVC5路由?非常好,它适用于路由。知道如何允许文件扩展名吗/Files/test/path/
有效,但/Files/test/path/somefile.pdf
无效。我注意到它适用于长路径,但对于像文件/p/test.pdf
这样的短路径,它是404。