【问题标题】:How do I set up my Routing for my MVC Page?如何为我的 MVC 页面设置路由?
【发布时间】:2016-03-27 14:22:47
【问题描述】:

问候 MVC 新手 ... 我正在创建我的第一个 MVC 应用程序,并按如下方式创建它:

CustomUtilities/Controllers/GCItemRetrievalController.cs
CustomUtilities/Views/GCItemRetrieval/GCRetrieve.cshtml
CustomUtilities/Views/Web.config

我想在我的浏览器中调出“GCRetrieve.cshtml ...但我不断收到 404 错误

http://mainsite/CustomUtilities/GCItemRetrieval/GCRetrieve 

我做错了什么?我在主系统上的一个单独文件夹中为控制器、模型和视图创建了文件夹。

【问题讨论】:

  • ...您在GCItemRetrievalController 中有GCRetrieve 操作吗? CustomUtilities 是 IIS 中的虚拟目录?
  • 我想我明白你的意思了,所以...我有两个动作,一个 index() 和一个 SubmitForm,所以,我需要一个 GCRetrieve 动作吗?正确,我将 View ... GCRetrieve 放在 Index 控制器上。就像我说的我是新手
  • 是的,@JasonEvans 在那里解释得很清楚,那里有很多问题和答案。
  • 你添加了注册路由方法了吗?您的路线需要在路线集合中注册。

标签: c# asp.net-mvc model-view-controller asp.net-mvc-routing


【解决方案1】:

您的控制器应如下所示:

public class GCItemRetrievalController : Controller
{
    public ActionResult GCRetrieve()
    {
        return View();
    }
}

当您导航到以下网址时:

http://mainsite/CustomUtilities/GCItemRetrieval/GCRetrieve

它应该找到控制器的GCRetrieve 方法并执行它。 return View() 调用将查找名为 GCRetrieve.cshtml 的 .cshtml 文件,因为这是方法的名称。

【讨论】:

  • 这是我的问题,我创建了 CustomUtilities 文件夹,并在其中创建了 /Models /Views、/Controllers/GCRetrieval,当我在 GCRetrieval 中创建操作/视图时,视图位于 /View 之外文件夹 /CustomUtilities 不在 /CustomUtilites/Views 中,因此我将 View 和 Web.config 移动到 /CustomUtlities/Views ... 注意,我试图离开 View 但它仍然不起作用
  • 我建议您使用由 ASP.NET MVC 项目模板为您生成的默认 Views 文件夹,而不是创建新的自定义文件夹。您不需要创建这些客户文件夹,因为默认的 Views 文件夹结构就足够了。是否有充分的理由需要创建一个新文件夹,例如CustomUtilities\Views?
  • 我同意,我还能在 /CustomUtilites 中创建 GCRetrieval 控制器吗?
  • 可以,但这意味着必须创建自己的视图引擎类,该类继承自RazorViewEngine,然后您可以配置视图引擎可以扫描哪些路径以查找cshtml 文件。老实说,这不是我熟悉的东西。但我相信您可以通过 Google 搜索到很多示例。
【解决方案2】:
 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

【讨论】:

    【解决方案3】:

    当您创建 MVC 应用程序时,会在 App_Start 目录中创建一个名为 RouteConfig.cs 的类文件。它的默认路由为

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

    在上面的默认路由中: 如果你想调用一个视图 CustomUtilities/Views/GCItemRetrieval/GCRetrieve.cshtml

    我认为 CustomUtilities 是您的项目名称,然后使用以下

    http://mainsite/GCItemRetrieval/GCRetrieve 那是 [域]/[控制器名称]/[动作名称]

    默认路由详情可以参考http://www.niceonecode.com/Q-A/DotNet/MVC/routing-in-mvc-4/20190

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 2018-11-24
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      相关资源
      最近更新 更多