【问题标题】:The resource cannot be found MVC4找不到资源MVC4
【发布时间】:2014-09-18 06:20:46
【问题描述】:

我已将我的网站上传到服务器。并且发生了错误。我的网站网址是http://vintageoverseas.org/。这是一个网址为http://vintageoverseas.org/en/Blog的页面,但是当我点击read more按钮Blog title然后The resource cannot be found.在此操作名称为BlogPost,当我将其更改为Post时,此错误发生为

The view 'Post' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Post.aspx
~/Views/Home/Post.ascx
~/Views/Shared/Post.aspx
~/Views/Shared/Post.ascx
~/Views/Home/Post.cshtml
~/Views/Home/Post.vbhtml
~/Views/Shared/Post.cshtml
~/Views/Shared/Post.vbhtml 

我的控制器名称是Home,它的代码是

  public ActionResult BlogPost(long? Id)
        {
            BlogModel blog = new BlogModel();
            blog = b.GetBlogDetail(Id.ToString());
            List<TagModel> tags = new List<TagModel>();
            tags = b.GetBlogTags(blog.blog_id.ToString());
            blog.tag_name_list = tags.Select(m => m.tag_name).ToList();
            List<ImageModel> image = new List<ImageModel>();
            image = b.GetBlogImages(blog.blog_id.ToString());
            blog.img_path_list = image.Select(m => m.path).ToList();
            blog.parent_cat_id = b.GetMainParent(blog.cat_id);
            return View(blog);
        }

而视图名称是BlogPost

而路由是

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Custom",
                url: "en/{action}/{id}/{Title}",
                defaults: new { controller = "Home", action = "Index", 
                    id = UrlParameter.Optional, Title = UrlParameter.Optional, en = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            ); 
        }

【问题讨论】:

  • 你有一个名字属性来装饰你的动作吗?或者您是来自Post 操作的RedirectToAction 呼叫吗?
  • en 用于路由@AndreiV,并且没有名为“post”的操作
  • 您是否考虑过名称为“Post.XXX”的视图不存在的可能性?看起来错误消息是这样说的......您的要求并不完全清楚 - 我假设您知道默认视图名称是操作的名称......所以不确定您正在寻找什么帮助。

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing


【解决方案1】:

您确定您的 BlogPost 操作位于 HomeController.cs 中吗?还是有一个名为“en”的控制器?

您可能需要在您的第一个路由中使用{controller} 参数,然后修改您的锚点以将控制器用作@Html.ActionLink("Read More...", "BlogPost", "ControllerName", new {id="123", null);

尝试使用路由调试器 - 安装后可以使用 web.config 开关打开和关闭它:https://www.nuget.org/packages/routedebugger

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2015-05-03
    • 2013-01-03
    • 2014-03-20
    • 2015-04-13
    • 2021-07-06
    相关资源
    最近更新 更多