简体   繁体   English

现有项目中的ASP.net MVC 5区域不起作用

[英]ASP.net MVC 5 Areas in existing project not working

I've been trying to set up a new Area in my current project following details instructions but I'm not able to get a successful result. 我一直在尝试按照详细说明在当前项目中设置一个新区域,但无法获得成功的结果。

Could somebody help me, please? 有人可以帮我吗? Here is my code so far: 到目前为止,这是我的代码:

RouteConfig.cs RouteConfig.cs

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

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

            routes.MapRoute(
                name: "Main_default",
                url: "{controller}/{action}/{id}",
                defaults: new {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional },
                namespaces: new string[] { "MesaServicio.Controllers" }
            );


            routes.MapRoute(
               name: "signin-google",
               url: "signin-google",
               defaults: new
                   {
                       controller = "Account",
                       action = "ExternalLoginCallback"
                   },
                namespaces: new string[] { "MesaServicio.Controllers" }
               );
        }
    }
}

AdminAreaRegistration.cs AdminAreaRegistration.cs

using System.Web.Mvc;

namespace MesaServicio.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { controller="Admin", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MesaServicio.Areas.Admin.Controllers" }
            );
        }
    }
}

AdminController.cs AdminController.cs

using System.Linq;
using System.Web.Mvc;
using MesaServicio.Models;

namespace MesaServicio.Areas.Admin.Controllers
{
    public class AdminController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: Admin/Admin
        public ActionResult Prueba()
        {
            return View(db.Corps.ToList());
        }
    }
}

Index.cshtml Index.cshtml

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

Area files structure screenshot 区域文件结构截图

File structure 档案结构

Global.asax.cs Global.asax.cs

using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MesaServicio
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

Link trigger 链接触发

<li><a href="@Url.Action("Index","Admin", new {area = "admin" })"><i class="fa fa-circle-o"></i> Admin</a></li>

RESULT 结果

https://localhost:44306/admin/ https:// localhost:44306 / admin /

Server Error in '/' Application. “ /”应用程序中的服务器错误。

No parameterless constructor defined for this object. 没有为此对象定义无参数构造函数。

The method Index() does not exist in AdminController 方法Index()在AdminController中不存在

The only method you have is Prueba(), which does not receive parameter "area". 您拥有的唯一方法是Prueba(),该方法不接收参数“ area”。

You should add something like this 您应该添加这样的内容

    public ActionResult Index(string area)
            { 
                //Do whatever you want
                return View();
            }

AdminController doesn't have an Index action, it instead has a Prueba action. AdminController没有Index动作,而是具有Prueba动作。 Try renaming method Prueba to Index . 尝试将Prueba重命名为Index

You've defined your default route for the Area to use the "Index" action: 您已经为区域定义了默认路线以使用“索引”操作:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { controller="Admin", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "MesaServicio.Areas.Admin.Controllers" }
    );
}

Which you have not defined in your Admin Area controller: 您尚未在“管理区域”控制器中定义的项:

namespace MesaServicio.Areas.Admin.Controllers
{
    public class AdminController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: Admin/Admin
        public ActionResult Prueba()
        {
            return View(db.Corps.ToList());
        }
    }
}

Either define the "Index" action for that Controller, or change the default to an action that does exist, which in your case the only defined action is "Prueba" 为该控制器定义“索引”操作,或将默认设置更改为确实存在的操作,在您的情况下,唯一定义的操作是“ Prueba”

In an ASP.NET MVC area application, you can link within an area as you would in any MVC application. 在ASP.NET MVC区域应用程序中,可以像在任何MVC应用程序中一样在区域内链接。 For example, you can call the ActionLink method, or you can call any other routine that takes a controller or action name (such as the RedirectToAction method). 例如,您可以调用ActionLink方法,也可以调用任何其他具有控制器或操作名称的例程(例如RedirectToAction方法)。

Just to complete the possibilities for others: 只是为了完成其他人的可能性:

You are referencing action Index in Admin controller which does not exist, so you have three possibilities. 您正在管理控制器中引用不存在的操作索引,因此有三种可能性。

  1. Rename action Prueba to Index 将动作Prueba重命名为Index
  2. Add new Action called Index 添加名为索引的新操作
  3. Add ActionName attribute so you let MVC know what is happening 添加ActionName属性,以便让MVC知道正在发生什么

     [ActionName("Index")] public ActionResult Prueba() { return View(db.Corps.ToList()); } 

You can try like this, its worked for me 你可以这样尝试,对我有用

 routes.MapRoute(
                  name: "AdminRoute",
                    url: "Admin/{controller}/{action}/{id}",
                    defaults: new { action = "Index", controller = "Login", id = UrlParameter.Optional },
                    namespaces: new[] { "Admin.Areas.DESK.Controllers" }
              );
  • Copy and paste this code above the default route in Routeconfig.cs 将此代码复制并粘贴到Routeconfig.cs中的默认路由上方
  • Remove the AreaRegistration.cs from project 从项目中删除AreaRegistration.cs
  • Clean the project,Rebuild and run.. 清理项目,重建并运行。

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

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