微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

向项目添加新页面

如何解决向项目添加新页面

我有一个使用C#的MVC项目。

我只使用一个视图Views / Home / Index.cshtml来完成我需要应用程序执行的大部分工作。

今天,我被要求添加一个页面,该页面将用作“管理员”类型的页面,以允许对记录进行一些基本操作。

我在理解如何导航到Home / Index.cshtml之外的页面时遇到了麻烦,实际上,我什至没有在浏览器中导航到该页面,因为这是认路由,所以该网址看起来像:http: // localhost:51225 / Meetings / Agenda /-这就是我看到Index.cshtml页面的方式。

到目前为止,我已经完成了在HomeController中的代码,将代码添加到了索引视图下面:

    // GET: Home  
    public ActionResult Index()
    {
        return View();
    }

    //GET: Admin 
    public ActionResult Admin()
    {
        return View();           -- I right clicked,and added a new view named "Admin"
    }

我的文件夹现在看起来:

Views
    Home
        Admin.cshtml
        Index.cshtml
        

我没有更改RouteConfig类:

public class RouteConfig
{
    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
                );
    }
}

我仍然可以打开我的应用程序,并在转到时看到Index.cshtml:

http://localhost:51225/Meetings/Agenda

但是我不知道如何访问Admin.cshtml 到目前为止,我知道这不是通过简单地在末尾添加Admin

来实现的。
http://localhost:51225/Meetings/Agenda/Admin

http://localhost:51225/Meetings/Agenda/Home/Admin

http://localhost:51225/Meetings/Agenda/Home/Admin.cshtml

是否可以寻求帮助,以了解如何以及如何解决我的解决方案中的内容,以便导航到Home文件夹中索引的另一个页面

解决方法

ASP.NET MVC的默认路由约定为Controllername / Action / parameter。 举例来说,假设您有一个类似Products/Create的路由,ASP.NET将在ProductsController中搜索名为Create的Action,并且视图将位于Create.cshtml目录内的Products

我建议您遵循该约定并创建一个AdminController并将Index操作放在控制器上。您可以通过localhost:51225/Admin/Index进行访问。对于视图,约定是它搜索与操作名称相同的视图,即创建一个名为Admin的文件夹,并将Index.cshtml放入其中

,

在默认路由之前,将自定义路由添加到Route配置中的RegisterRoutes方法中:

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

最终RouteConfig:

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

            routes.MapRoute(
                name: "AgendaRoute",id = UrlParameter.Optional }
            );

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

结果:

http://localhost:51225/Meetings/Agenda => Index.html
http://localhost:51225/Meetings/Agenda/index => Index.html
http://localhost:51225/Meetings/Agenda/index/1 => Index.html

http://localhost:51225/Meetings/Agenda/admin => Admin.html
http://localhost:51225/Meetings/Agenda/admin/1 => Admin.html
,

您始终可以通过/ ControllerName / ViewName / ParametersIfNeeded访问任何内容。我建议您在导航栏上添加一个仅对管理员可见的按钮,以便只有他们才能访问该页面。在您的情况下,可以使用http:// localhost:51225 / Home / Admin访问admin视图。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。