2017-06-02 100 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文件。

添加该文件,它应该工作