Rounting在ASP MVC的地区 - 以问题更多领域 [英] Rounting in asp mvc areas - issue with more areas

查看:108
本文介绍了Rounting在ASP MVC的地区 - 以问题更多领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

路由让我疯了thees天:(
我有两个方面:CityPage和工作
在城市页面我有那个导航到工作区的链接列表。
这是code行动和路由CityPage:

 公众的ActionResult指数(字符串市,字符串全国code)
        {
            返回查看();
        }在CityPageAreaRegistration.cs类:
context.MapRoute(
                空值,
                CityPage / {国家code} / {城市},
                新{面积=CityPage,控制器=家,行动=索引,城市=}
            );

网址,我得到的这边独好


  

的http://本地主机/ CityPage /英国/伦敦


从CityPage指数我可以导航到任务列表(工作/家庭/索引

  @ Html.ActionLink(乔布斯,索引,家,新{面积=作业},NULL)

这是从工作区code:

 公众的ActionResult指数()
        {
            返回查看();
        }
context.MapRoute(
                空值,
                {地区} / {国家code} / {城市} /招聘/ {控制器} / {行动},
                新{面积=作业,控制器=家,行动=索引,
                      国家code =UK,
                      城市=伦敦
                }
            );

网址,我来到这里仍然有效。


  

的http://本地主机/ CityPage /英国/伦敦/招聘


不过,该网页我有链接的工作细节(工作/家庭/显示

  @ Html.ActionLink(作业示例,展,家,新{面积=作业,=的jobId 8765},NULL)

和URL,我得到的是


  

的http://本地主机/ CityPage /英国/伦敦/招聘/首页/显示?=的jobId 8765


 我已尝试添加路由这样的            context.MapRoute(
                工作,
                {}区/工作/ {}的jobId
                新
                {
                    面积=作业,
                    控制器=家,
                    行动=索引,
                    国家code =RS,
                    城市=克拉古耶瓦茨
                    的jobId =
                }
            );

但它不工作。我不知道我做错了:(
我试图让网址是一样的东西。


  

的http://本地主机/ CityPage /英国/伦敦/招聘/ 8765


我还在学习有关路由。但地区让我...

我不添加任何路由的Global.asax我认为我需要写在路由领域routing.cs类code,对吗?


解决方案

它看起来像你pretty关闭。

我创造了这样的路线:

  context.MapRoute(
            Job_Locator
            {国家code} / {城市} /招聘/ {}的jobId
            新
            {
                面积=作业,
                控制器=家,
                行动=秀
            }
        );

然后像这样的ActionLink的:

  @ Html.ActionLink(测试,索引,家,新{面积=工作,国家code =UK,城市=伦敦=的jobId 10},NULL)

当操作是:

 公众的ActionResult指数(字符串市,字符串全国code中,int的jobId)
{
    返回查看();
}

请在我创建了一个名为作业的新区域的头脑和我与首页行动HomeController的有

Routing make me crazy thees days :( I have two areas: CityPage and Job On the city page I have a list of links that navigate to Job area. This is code for action and routing for CityPage:

public ActionResult Index(string city, string countryCode)
        {
            return View();
        }

In CityPageAreaRegistration.cs class:
context.MapRoute(
                null,
                "CityPage/{countryCode}/{city}",
                new { area = "CityPage", controller = "Home", action = "Index", city = "" }
            );

URL that I get here is fine

http://localhost/CityPage/UK/London

From CityPage index I can navigate to Jobs list (Job/Home/Index)

@Html.ActionLink("Jobs", "Index", "Home", new { area = "Job" }, null)

This is code from Job area:

public ActionResult Index()
        {
            return View();
        }
context.MapRoute(
                null,
                "{area}/{countryCode}/{city}/Job/{controller}/{action}",
                new { area = "Job", controller = "Home", action = "Index",
                      countryCode = "UK",
                      city = "London"
                }
            );

URL that I get here is still fine

http://localhost/CityPage/UK/London/Job

But on that page I have link to job details (Job/Home/Show)

@Html.ActionLink("Job Example", "Show", "Home", new { area = "Job", jobId = 8765 }, null)

And URL that I get is

http://localhost/CityPage/UK/London/Job/Home/Show?jobId=8765

I have tried to add routing like this

            context.MapRoute(
                "Jobs",
                "{area}/Job/{jobId}",
                new
                {
                    area = "Job",
                    controller = "Home",
                    action = "Index",
                    countryCode = "RS",
                    city = "Kragujevac",
                    jobId = ""
                }
            );

But it doesn't work. I don't know what am I doing wrong :( URL that I am trying to get is something like

http://localhost/CityPage/UK/London/Job/8765

I still learning about routing. But areas make me ...
I don't add any routing to Global.asax I think that I need to write routing code in areas routing.cs classes, am I right?

解决方案

It looks like you were pretty close.

I created a route like this:

context.MapRoute(
            "Job_Locator",
            "{countryCode}/{city}/Job/{jobId}",
            new
            {
                area = "Job",
                controller = "Home",
                action = "Show"
            }
        );

And then an actionlink like this:

@Html.ActionLink("Test", "Index", "Home", new { Area = "Job", countryCode = "UK", city = "London", jobId = 10 }, null)

Where the action is:

public ActionResult Index(string city, string countryCode, int jobId)
{
    return View();
}

Keep in mind that I created a new area called "Job" and I have a HomeController there with the Index action

这篇关于Rounting在ASP MVC的地区 - 以问题更多领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆