有人可以解释一下MVC 2中路由器与控制器的关联方式吗?目前,我在/Controllers/HomeController.cs中有一个控制器和一个视图/Home/Index.aspx.
我的路线注册方法如下:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}
Run Code Online (Sandbox Code Playgroud)
如果我请求URL:http:// localhost/Home/Index,则HomeController.Index()正确处理请求.
但是,对于我的生活,我无法弄清楚url/Home/Index如何指向HomeController.据我所知,视图aspx没有引用HomeController,HomeController没有引用视图,路由表也没有明确提到HomeController.这神奇地发生了什么?当然我错过了一些明显的东西.
然后
所以想象一下,我正在使用MVC Web应用程序构建一个多用户地牢系统.为了描述玩家可以探索的区域,系统可以包含许多地图,其中包括房间和门 - 其中门连接两个房间.考虑系统的创作部分.创建地图很简单 - 我需要以下网址:
/Author/Maps (an index of my maps) /Author/Maps/Create (a new Map) /Author/Maps/Detail/3 (show Map details) /Author/Maps/Edit/3 (edit map details)
使用路由方案:/ Author/{controller}/{action}/{ID}
这是我需要帮助的房间的URL.在创建一个新的房间时,我需要知道我正在创建它的地图.
/Author/Rooms/CreateIn/[mapID] ?
然后编辑房间的详细信息:
/Author/Rooms/Edit/[roomID] /Author/Rooms/Detail/[roomID]
这种路由方案是否有效?列出Map的所有房间的视图是否应该是Rooms控制器上的"Index"操作,传入MapID或Map控制器上的"Rooms"操作?
谢谢.
theory model-view-controller asp.net-mvc-routing hierarchical-data
我正在使用带有区域的MVC 2.为了测试路由,我使用的是MvcContrib.
这是测试代码:
[Test]
public void Home()
{
MvcApplication.RegisterRoutes(RouteTable.Routes);
"~/".ShouldMapTo<HomeController>(x => x.Login("Nps"));
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何调用存储在Regions中的路由定义.调用AreaRegistration.RegisterAllAreas()不是一个选项,因为它给出了一个例外.
谢谢Revin
当运行nopcommerce 2.4.0时,我得到的链接localhost:7725/c/2/computers不是localhost:7725/categories/2/computers
我不知道代码在哪里将类别转换为c
请帮我通过nopcommerce学习MVC.
我的MVC应用程序存在一些问题,如果我的应用程序部署到域根以外的任何其他地方,则使用基于逻辑的数据驱动URL(用于路由).
我尝试使用各种Request.Url属性无济于事.
假设我将应用程序部署到www.mydomain.com/myapp/甚至是www.mydomain.com/myapp/subapp/.在这种情况下,我怎么能得到/myapp/和/myapp/subapp/分别.即使用户处于完全不同的页面上,例如/myapp/Users/Recent/?
我需要这样,所以我可以为所有数据驱动的URL添加前缀,这样我的应用程序在不在域根目录时仍可正常工作.
谢谢.
我有这个控制器:
public class ProfileController : Controller
{
public ActionResult Index( long? userkey )
{
...
}
public ActionResult Index( string username )
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
如何为此操作定义MapRoute,如下所示:
mysite.com/Profile/8293378324043043840
这必须先行动
mysite.com/Profile/MyUserName
这必须要进行第二次行动
我有这条路线进行第一次行动
routes.MapRoute( name: "Profile" , url: "Profile/{userkey}" , defaults: new { controller = "Profile" , action = "Index" } );
Run Code Online (Sandbox Code Playgroud)
我需要添加另一个MapRoute吗?或者我可以为这两个动作更改当前的MapRoute吗?
我正在使用之前使用ASP.NET Web Forms构建的网站,现在使用ASP.NET MVC构建.
我们上周制作了新的MVC版本.
但是旧的登录网址www.website.com/login.aspx已被许多用户收藏,他们仍然使用它,因此他们得到404错误.
所以我想知道哪个是将用户从旧网址重定向到新的mvc网址的最简单最好的方法,即www.website.com/account/login
就像这个登录网址一样,我希望用户可能还有其他几个网址的书签,那么处理这个问题的最佳方法是什么?
如果我有一个捆绑包如:
bundles.Add(new ScriptBundle("~/foo/bar").Include(
"~/Scripts/foo.js"));
Run Code Online (Sandbox Code Playgroud)
以及如下的路线:
routes.MapRoute(
"Foo", // Route name
"foo/bar",
new
{
controller = "Foo",
action = "Bar"
});
Run Code Online (Sandbox Code Playgroud)
哪一个优先?
浏览器会返回ScriptBundle还是ActionResult?
asp.net asp.net-mvc asp.net-mvc-routing bundling-and-minification asp.net-mvc-5
我正在创建一个可重用的方法来检查我的模型并自动构建ActionLink用于分页的URL(via ).我模型上的一个属性是string[](对于多选选择列表),它完全有效.URL的一个例子是:https://example.com?user=Justin&user=John&user=Sally.
但是,正如类型的名称所暗示的那样,RouteValueDictionary实现IDictionary因此它不能多次接受相同的键.
var modelType = model.GetType();
var routeProperties = modelType.GetProperties().Where(p => Attribute.IsDefined(p, typeof(PagingRouteProperty)));
if (routeProperties != null && routeProperties.Count() > 0) {
foreach (var routeProperty in routeProperties) {
if (routeProperty.PropertyType == typeof(String)) {
routeDictionary.Add(routeProperty.Name, routeProperty.GetValue(model, null));
}
if (routeProperty.PropertyType == typeof(Boolean?)) {
var value = (Boolean?)routeProperty.GetValue(model, null);
routeDictionary.Add(routeProperty.Name, value.ToString());
}
//The problem occurs here!
if (routeProperty.PropertyType == typeof(string[])) {
var value = (string[])routeProperty.GetValue(model);
foreach (var v in value) { …Run Code Online (Sandbox Code Playgroud) 我只需将ASP.Net 4.5.2应用程序降级到ASP.Net 4.0.当然,这会带来问题,例如未正确安装的引用.我已经解决了其中的一些问题,但我无法理解错误:
CS106'RouteCollection'不包含'MapMvcAttributeRoutes'的定义, 并且没有 可以找到接受类型 'RouteCollection' 的第一个参数的扩展方法'MapMvcAttributeRoutes'
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "device", action = "view", id = UrlParameter.Optional });
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道我在这里要做什么吗?
额外信息
我使用的命名空间:
Visual Studio社区2015
asp.net-mvc ×5
asp.net ×4
c# ×2
mvccontrib ×1
nopcommerce ×1
reference ×1
theory ×1
unit-testing ×1
webforms ×1