Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET无法在启动时路由到视图_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET无法在启动时路由到视图

C# ASP.NET无法在启动时路由到视图,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我是.NET和C#的新手,在应用程序启动时无法路由到我的单视图文件。关于路由,我一定有些不明白,因为浏览器中的URL: /Views/LoadCustomerAndDisplay/Index.cshtml 对我来说似乎是对的。但是,当我构建并运行应用程序时,我会遇到404错误。以下是文件结构的图片: 以下是浏览器错误: RouteConfig: using System; using System.Collections.Generic; using System.Linq; using

我是.NET和C#的新手,在应用程序启动时无法路由到我的单视图文件。关于路由,我一定有些不明白,因为浏览器中的URL:

/Views/LoadCustomerAndDisplay/Index.cshtml
对我来说似乎是对的。但是,当我构建并运行应用程序时,我会遇到404错误。以下是文件结构的图片:

以下是浏览器错误:

RouteConfig

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

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
    public class LoadCustomerAndDisplayController : Controller 
    {
        // GET: LoadCustomerAndDisplay
        public ActionResult Index()
        {
            Customer objCustomer = new Customer();
            objCustomer.Id = 1001;
            objCustomer.CustomerCode = "C";
            objCustomer.Amount = 900.78;
            return View(objCustomer);
        }
    }
}
加载客户和显示控制器

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

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
    public class LoadCustomerAndDisplayController : Controller 
    {
        // GET: LoadCustomerAndDisplay
        public ActionResult Index()
        {
            Customer objCustomer = new Customer();
            objCustomer.Id = 1001;
            objCustomer.CustomerCode = "C";
            objCustomer.Amount = 900.78;
            return View(objCustomer);
        }
    }
}

我缺少什么?

您无法通过浏览器访问
.cshtml
。正确的URL应该是
/LoadCustomerAndDisplay/Index
。它使用
LoadCustomerAndDisplay
控制器的
Index
操作

作为旁注,您可能希望创建一个更通用的控制器名称,如
Customers
,并在该控制器下创建与您的客户实体相关的任何操作。

在MVC中,将虚拟“页面”映射到物理文件,默认路由是
{controller}/{action}/{id}

默认路由表包含单个路由(名为default)。这个 默认路由将URL的第一段映射到控制器名称, 指向控制器操作的URL的第二段和第三段 段到名为id的参数

因此,在您的例子中,您正在调用
/views/loadCustomerAddislPay/index.cshtml
,但是使用MVC您不引用物理文件,而是调用
{controller}/{action}

电话:

它应该会起作用。(索引操作是默认操作,可以省略)


这假设您没有对
路由图
进行任何更改,应该是这样的:

    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 = "" }  // Parameter defaults
        );
    }
public class LoadCustomerAndDisplayController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

我假设您的
LoadCustomerAndDisplay
控制器看起来像:

    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 = "" }  // Parameter defaults
        );
    }
public class LoadCustomerAndDisplayController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
按照我的惯例,控制器应该是nammed
xxxController
,查看您的文件名,情况可能并非如此

发件人:

请注意,控制器名称的第一部分在中高亮显示 “添加控制器”对话框。每个控制器名称必须以 后缀控制器。例如,可以创建名为的控制器 ProductController,但不是名为Product的控制器

(无可否认,这是我能找到的最好的引文版本)



最后,视图应该位于一个名为控制器的文件夹中,没有控制器后缀,因此在您的情况下,不应该直接访问cshtml。通过操作方法访问它。好的,我知道了,我不是用Index()做的吗?你应该尝试访问的url是
LoadCustomerAndDisplay/Index
你能粘贴你的控制器代码吗?我更新了我的答案,让它看起来有点奇怪,当我调用/LoadCustomerAndDisplay时,我得到了同样的错误,显式地-
/LoadCustomerAndDisplay/Index
?就可以了,只是快速地测试一些东西我已经添加了默认的routeconfig,如果您(或其他开发人员)没有接触它,看起来应该是这样的。是不是因为我没有指定控制器?