2017-06-02 222 views
0

我正在學習asp.net,並且無法解決一段時間內相當簡單的問題。Asp.net MVC路由w /字符串參數

有RouteConfig.cs與以下內容的文件:

public class RouteConfig { 
    public static void RegisterRoutes(RouteCollection routes) { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapMvcAttributeRoutes(); 

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

,並獲得與內容控制器:

... 
[Route("image/show/{uuid}")] 
public ActionResult Show(string uuid) { 
    return View(uuid); 
} 
... 

And the result when I try to open this url

在此先感謝您的答覆!

+0

沒有發現您已經參與屬性路由的路徑。 –

+0

好,但爲什麼呢?如果我將Show fn-s參數更改爲int即:Show(int uuid)。然後,相同的URL - 在結束時使用一個數字作爲參數 - 起作用,並且路由很好。所以在我看來,問題的根本原因是參數類型是字符串... – Joooe

回答

1

它的下面一行創建問題

return View(uuid); 

這條線走的ViewResult視圖(字符串的viewName)超載控制器。它正在考慮將您傳入的uuid變量作爲視圖名並嘗試按該名稱查找視圖文件。

作爲一種變通方法,您可以更改該行

return View((object)uuid); 

這將需要正確的過載的ViewResult視圖(對象模型) 或存儲在viewbag的uuid並返回視圖

return View(); 

當然,你的View/Image目錄中仍然需要Show.cshtml文件。

+0

你是一天中的人!謝謝,它現在正在工作! – Joooe

-1

錯誤表示它無法在Views/Images/sdfsd.cshtml中找到cshtml文件。

添加該文件,它應該工作