在 asp mvc 区域中路由 - 更多区域的问题
这几天路由让我抓狂:( 我有两个区域:CityPage 和 Job 在城市页面上,我有一个导航到工作区域的链接列表。 这是 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 很好
从 CityPage 索引我可以导航到职位列表 (Job/Home/Index
)
@Html.ActionLink("Jobs", "Index", "Home", new { area = "Job" }, null)
这是来自工作区域的代码:
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 仍然没问题
但在该页面上我有链接工作详细信息 (Job/Home/Show
)
@Html.ActionLink("Job Example", "Show", "Home", new { area = "Job", jobId = 8765 }, null)
和我得到的 URL 是
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 = ""
}
);
但这不起作用。我不知道我做错了什么:( 我想要获取的网址类似于
我还在学习路由。但领域让我...
我没有向 Global.asax 添加任何路由 我认为我需要在routing.cs 类中编写路由代码,对吗?
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
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
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你们已经很接近了。
我创建了一个像这样的路线:
然后是一个像这样的操作链接:
操作在哪里:
请记住,我创建了一个名为“Job”的新区域,并且那里有一个带有
Index
操作的 HomeControllerIt looks like you were pretty close.
I created a route like this:
And then an actionlink like this:
Where the action is:
Keep in mind that I created a new area called "Job" and I have a HomeController there with the
Index
action我一直在追随你的疯狂。我想建议您尝试称为 T4MVC 的东西。它作为 NuGet 包提供,我认为现在是 MvcContrib 的一部分。
我是 T4MVC 的忠实粉丝。它使将 URL 路由到操作方法变得更加容易,并且消除了许多魔术字符串。
以下是我们应用程序“公共”区域中的路由定义示例:
请注意,区域、控制器或操作名称没有魔法字符串。所有这些内容都是由 T4MVC 强类型化的。最重要的部分是:看看我们如何生成这些页面的链接:
有一些 ActionLink 重载采用 ActionResult 参数。 T4MVC 为您提供了一个强类型的 ActionResult 方法,您可以在此重载中使用它。传递给该方法的参数将与您的路由定义进行映射。
当我对路由和操作方法进行编码时,我只担心路由中 URL 参数的名称与它们映射到的操作方法的参数相匹配。因此,这是 FeaturesController 上 ForPreview 方法的签名:
这种方法使区域之间的交叉链接变得更加容易,请尝试一下。
更新
哎呀,上面的路由定义默认情况下不会编译。我忘了我的项目中有这个扩展方法:
I've been following your craziness. I'd like to suggest you try something called T4MVC. It is available as a NuGet package, and I think is now part of MvcContrib.
I am a big fan of T4MVC. It makes routing URLs to action methods MUCH easier, and gets rid of a lot of magic strings.
Here is an example of a route definition in the "Common" area on our app:
Notice there are no magic strings for area, controller, or action names. All of this stuff is strongly-typed by T4MVC. The greatest part is this: look how we can generate a link to these pages:
There are some ActionLink overloads that take an ActionResult parameter. T4MVC gives you a strongly-typed ActionResult method that you can use in this overload. Parameters passed to the method will map with your route definitions.
When I'm coding routes and action methods, I only worry that the name of the URL parameters in my route match the arguments to the action method they are mapped to. So here is the signature of the ForPreview method on FeaturesController:
This approach makes cross linking between areas a lot easier, give it a shot.
Update
Oops, the above route definition won't compile by default. I forgot I have this extension method in the project: