Asp.Net MVC 3 - 如何将视图添加/映射到项目?

发布于 2024-12-15 11:53:29 字数 1796 浏览 0 评论 0原文

我创建了一个新的 MVC 3 项目。我运行了该项目并加载了主页。然后我添加了一个名为“discussion.cshtml”的新视图。

我的 Controllers 文件夹中有 1 个控制器: HomeController

EDIT 我之前拼错了 Discussion,但它在我的项目中没有拼写错误。问题仍然存在。

新视图的设置方式如下:View ->首页-> Discussion.cshtml

我将此代码添加到 HomeController

 public ActionResult Discussion()
         {
             return View();
         }

问题: 当我点击运行时,视图未加载。我收到 404 错误。索引页面使用此 url“localhost:5553”加载。但我尝试了 "localhost:5553/discussion.cshtml" 但找不到它。如何映射/路由到视图。我不确定发生了什么事,我觉得我错过了一些简单的事情。

Global.ascx 页面有通常的默认代码(如果有帮助的话):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

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

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

I created a new MVC 3 project. I ran the project and it loaded the home page. I then added a new view called "discussion.cshtml".

I have 1 controller in the Controllers folder: HomeController

EDIT I misspelled Discussion before but it's not misspelled in my project. the problem persists.

the new view is setup in this fashion: View -> Home -> Discussion.cshtml

I added this code to the HomeController

 public ActionResult Discussion()
         {
             return View();
         }

Problem: The view did not load when I hit run. I got the 404 error. The index page loads with this url "localhost:5553". But I tried "localhost:5553/discussion.cshtml" and it can't find it. How do I map/route to a view. I'm not sure what's going on, I feel like I'm missing something simple.

The Global.ascx page has the usual default code if it helps:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

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

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长不大的小祸害 2024-12-22 11:53:29

您不能在 mvc 上执行类似 localhost:5553/discussion.cshtml 的操作。

右键单击“讨论”操作并选择“添加视图”。

如果您浏览 localhost:5553/Home/Discussion,则会路由到查看您在上面添加的内容。

you can not do like localhost:5553/discussion.cshtml" on mvc.

right click over the Discussion action and select Add View.

if you browse localhost:5553/Home/Discussion, that will route to the view you added above.

甩你一脸翔 2024-12-22 11:53:29

你好像有一个错字。尝试将 Discsussion.cshtml 重命名为 Discussion.cshtml

并尝试访问它 http://localhost:5553/home/discussion

You seem to have a typo. Try renaming Discsussion.cshtml to Discussion.cshtml

And try accessing it http://localhost:5553/home/discussion

夜深人未静 2024-12-22 11:53:29

对于仅返回 View() 的操作:

 public ActionResult Discussion()
 {
      return View();
 }

ASP.NET MVC 引擎将在与控制器同名的文件夹中查找与该操作同名的文件。在您的情况下,文件名的拼写不同。您也可以这样做:

 public ActionResult Discussion()
 {
      return View("Discsussion");
 }

这将与您当前的文件名匹配,但似乎是一个拼写错误,因此只需重命名您的文件即可。

With an action that just returns View():

 public ActionResult Discussion()
 {
      return View();
 }

The ASP.NET MVC engine will look for a file with the same name as the action in the folder with the same name as the controller. In your case, the file name is spelt differently. You could also do:

 public ActionResult Discussion()
 {
      return View("Discsussion");
 }

This will match your current file name, but it seems its a typo so just rename your file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文