简体   繁体   English

如何为我的MVC页面设置路由?

[英]How do I set up my Routing for my MVC Page?

Greetings New to MVC ... I am creating my first MVC Application, and I have created it as follows: MVC的新问候...我正在创建我的第一个MVC应用程序,并且创建过程如下:

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

I want to pull up "GCRetrieve.cshtml in my browser ... but I keep getting a 404 Error 我想在浏览器中提取“ GCRetrieve.cshtml ...,但我一直收到404错误

http://mainsite/CustomUtilities/GCItemRetrieval/GCRetrieve 

what am I doing wrong? 我究竟做错了什么? I created the folders for the controllers, models, and Views in a seperate folder on the main system. 我在主系统的单独文件夹中为控制器,模型和视图创建了文件夹。

Your controller should look something like this: 您的控制器应如下所示:

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

When you navigate to the following url: 当您导航到以下网址时:

http://mainsite/CustomUtilities/GCItemRetrieval/GCRetrieve

It should find the controller's GCRetrieve method and execute it. 它应该找到控制器的GCRetrieve方法并执行它。 The return View() call will look for a . return View()调用将查找。 cshtml file named GCRetrieve.cshtml , as that is the name of the method. 名为GCRetrieve.cshtml cshtml文件,因为这是方法的名称。

 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 }
        );
    }
}

When you create a MVC application a class file named as RouteConfig.cs is created in App_Start directory. 创建MVC应用程序时,将在App_Start目录中创建一个名为RouteConfig.cs的类文件。 It has default routing as 它的默认路由为

    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 }
        );
    }
}

In the above default routing: if you want call a view CustomUtilities/Views/GCItemRetrieval/GCRetrieve.cshtml 在上面的默认路由中:如果要调用视图,则CustomUtilities / Views / GCItemRetrieval / GCRetrieve.cshtml

I think CustomUtilities is your project name then use following 我认为CustomUtilities是您的项目名称,然后使用以下

http://mainsite/GCItemRetrieval/GCRetrieve that is [domanin]/[controllername]/[actionname] http:// mainsite / GCItemRetrieval / GCRetrieve即[domanin] / [controllername] / [actionname]

For default routing detail you can refer to http://www.niceonecode.com/QA/DotNet/MVC/routing-in-mvc-4/20190 有关默认路由的详细信息,请参阅http://www.niceonecode.com/QA/DotNet/MVC/routing-in-mvc-4/20190

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM