简体   繁体   中英

areas in asp.net mvc?

I have a Home page route in the root area:

routes.MapLocalizedRoute("HomePage",
                            "",
                            new { controller = "Home", action = "Index" },
                            new[] { "Nop.Web.Controllers" });

and an area called Xahoi

    namespace Nop.Web.Areas.Xahoi
{
    public class XahoiAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Xahoi";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {

            context.MapRoute(
            "Xahoi_default",
            "Xahoi/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            new[] { "Nop.Web.Areas.Xahoi.Controllers" });


        }
    }
}

when i call http://domain.com/xahoi/home -> it does get into Home controller in Xahoi Area but then it runs the Index view from Home controller in the root.

Also, suppose i want to change the call to just http://domain.com/xa-hoi , how can i do that ?

To specify a default controller simply add it into the array for the third parameter:

public override void RegisterArea(AreaRegistrationContext context)
    {

        context.MapRoute(
        "Xahoi_default",
        "Xahoi/{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new[] { "Nop.Web.Areas.Xahoi.Controllers" });


    }

The issue with it displaying the wrong view could be a namespace issue on the fourth parameter and it is running the code in the wrong controller... or that your view folder structure is structured in such a way that it isn't finding a view in the Areas\\Xahoi\\Views\\Home folder and is using an Index view from the root views folder. Maybe you have an Index view in the Views folder rather than it being in Views\\Home folder?

To determine if it's a namespace issue or not put a break point on your new controller (the one in the area) Index method and see if it get's hit when you visit the page.

Hope this helps

First of all, I'd suggest installing glimpse to get a better understanding how MVC works "behind the scene"

Basicly it will look for controllers and views in a couple of places, using glimpse will make this obvious :)

Do you actually have a view called index belonging to Home, in your Xahoi area? eg Areas\\Xahoi\\Views\\Home\\Index.cshtml

Else you can specify the Area property in your MapRoute, though I think that is what it does by default. The following mapping should also change your url, to what you where asking

public override void RegisterArea(AreaRegistrationContext context) {

  context.MapRoute( "Xahoi_default", "xa-hoi/{controller}/{action}/{id}", new { Area=AreaName controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Nop.Web.Areas.Xahoi.Controllers" }); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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