2014-11-14 74 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屬性。