2014-11-14 72 views
1

我有实现MVC中5路由故障在调试预期的网址(如http://localhost/Download/Blog/1cf15fe6033a489a998556fedeab20a2/Test/1cd15fe6033a489a998556fedeab20a2)导致下载控制器上的正确方法,然而,被称为didfid总是null。我究竟做错了什么?我也试图消除下载路线和具有以下属性定义控制器的路径:麻烦参数属性

[RoutePrefix("Download")] //on the controller 
[Route("{action}/{did:guid}/Test/{fid:guid}")] //on the Blog Action 

以下是我在我的RouteConfig.cs:

routes.MapRoute(
      name: "Download", 
      url: "Download", 
      defaults: new { controller = "Download", action = "Index" } 
     );    

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { 
       controller = "Home", 
       action = "Index", 
       id = UrlParameter.Optional 
      } 
     ); 

这里是我的控制器:

[Route("{action}/{did}/Test/{fid}")] 
public class DownloadController : Controller 
{ 
    public ActionResult Index() 
    { 
     return Redirect(HandleBadResponse(webResponse)); 
    } 

    [Route("{action}/{did}/Test/{fid}")]//nope 
    public ActionResult Blog(HttpRequestMessage request,string did,string fid) 
    { 

     string server = Request.ServerVariables["SERVER_NAME"]; 

     string pathStr = @"\\mypath\1cf15fe6033a489a998556fedeab20a2.xls"; 

     byte[] fileBytes = System.IO.File.ReadAllBytes(pathStr); 
     string fileName = "test.txt"; 
     return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName); 
    } 
+0

您在实际控制器上的操作与操作方法相同。这是一个错字吗? – 2014-11-14 14:16:00

+0

它不是一个错字,虽然我尝试了我在RouteConfig.cs文件中没有使用该路由列出的同样的事情,但都没有像我预期的那样工作,'fid'和'did'仍然是'null',但博客操作是呼吁下载控制器。 – Timigen 2014-11-14 14:18:44

回答

0

我得到了预期的结果通过添加以下到我的RouteConfig.cs我放在这条路线在我的RegisterRoutes方法的顶部,我想你应该从最detaile去d路由到最小的路由:

routes.MapRoute(
    name: "DownloadBlogAttachment", 
    url: "Download/Blog/{did}/fid/{fid}", 
    defaults: new { controller = "Download", action = "Blog"} 
); 

我也删除了我的控制器中的Route属性。