ASP.NET MVC 3.0路由行为 [英] ASP.NET MVC 3.0 Routing behaviour

查看:156
本文介绍了ASP.NET MVC 3.0路由行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对夫妇的动作控制器BlogController:

I have controller BlogController with a couple of actions:

1)Index(string id) - show all posts/show single post if parameter id specified
2)New() - add new post
3)Delete() - delete post
4)And some more another actions

所以,如果我在浏览器中键入的mysite /博客我能看到的所有帖子,如果我键入 mysite的/博客/ postnameid 我希望看到一个帖子。

So if i type in browser mysite/blog i could see all posts if i type mysite/blog/postnameid i want to see single post.

问题是当我键入 mysite的/博客/ postnameid 它的不工作资源无法找到),但如果我键入 mysite的/博客/指数/ postnameid 这种方式,它是工作。我如何才能让 mysite的/博客/ postnameid 为很好地工作。

the problem is when i type mysite/blog/postnameid it is not working (The resource cannot be found.), but if i type mysite/blog/index/postnameid this way it is working. How could i make mysite/blog/postnameidto work as well.

下面是 global.ascx

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

 routes.MapRouteLowercase(
               "Blog", // Route name
               "Blog/{action}/{id}", // URL with parameters
               new { controller = "Blog", action = "Index" } // Parameter defaults
            );

            routes.MapRouteLowercase(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );

比我改变它像

routes.MapRouteLowercase(
               "Blog", // Route name
               "Blog/{id}", // URL with parameters
               new { controller = "Blog", action = "Index" } // Parameter defaults
            );

mysite的/博客/ postnameid 工作,但所有其他操作,如新(),删除()停止后工作(资源不能找到。

the mysite/blog/postnameid working but all another actions like New(), Delete() stop working after that (The resource cannot be found. )

更新:
我忘了提,ID被蜇,不是int。所以从@Darin答案我改变了新{ID = @\\ w +} 新{ID = @\\ D +} 和所有接缝处工作,但现在当我输入博客/新例如,它被路由到显示/新 insteard 博客/新

UPDATE: I forgot to mention that id is sting, not int. so from @Darin answer i changed new { id = @"\w+" } to new { id = @"\d+" } and all seams to be working but now when i typed blog/new for example, it is routing to show/new insteard blog/new

推荐答案

这是一个坏主意,反对基于REST的约定有一个单独的控制器动作是做两件事情,当然违反了单一职责原则(列表中的所有帖子,如果没有ID指定,并显示给定后,如果指定id)。正确的方法是将有以下内容:

It is a bad idea and against RESTful conventions to have a single controller action that do 2 things and of course violating the single responsibility principle (list all posts if no id is specified and show a given post if an id is specified). The correct way would be to have the following:


  • /博客 => BlogController /索引=>列表中的所有帖子

  • /博客/ 123 => BlogController /显示(ID = 123)=>特定职位的展示细节

  • /博客/新 => BlogController /新()=>开始写一个新的职位

  • /博客/删除/ 123 => BlogController /删除(ID = 123)=>删除特定岗位

  • /blog => BlogController/Index => list all posts
  • /blog/123 => BlogController/Show(id = 123) => show details of a given post
  • /blog/new => BlogController/New() => start writing a new post
  • /blog/delete/123 => BlogController/Delete(id = 123) => delete given post

这将具有以下途径实现的:

which would be achieved with the following routes:

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

    routes.MapRoute(
        "Blog",
        "blog/{id}",
        new { controller = "Blog", action = "Show" },
        new { id = @"\d+" }
    );

    routes.MapRoute(
        "Default",
        "blog/{action}/{id}",
        new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
    );
}

注意的第一个定义所需的路由约束这表明什么形式的所有ID必须使路由引擎能够ID和动作名称之间的歧义。

Notice the required route constraint on the first definition which indicates what form all ids must be so that the routing engine could disambiguate between an id and an action name.

这正在如果要违背原则2我前面提到的,并有你想有一个轻微的调整将需要路由说:

This being said if you want to violate the 2 principles I mentioned earlier and have the routes you want a slight adaptation would be required:

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

    routes.MapRoute(
        "Blog",
        "blog/{id}",
        new { controller = "Blog", action = "Index" },
        new { id = @"\d+" }
    );

    routes.MapRoute(
        "Default",
        "blog/{action}/{id}",
        new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
    );
}

现在


  • /博客 => BlogController /索引(ID = NULL)=>列表中的所有帖子

  • /博客/ 123 => BlogController /索引(ID = 123)=>特定职位的展示细节

  • /博客/新 => BlogController /新()=>开始写一个新的职位

  • /博客/删除/ 123 => BlogController /删除(ID = 123)=>删除特定岗位

  • /blog => BlogController/Index(id = null) => list all posts
  • /blog/123 => BlogController/Index(id = 123) => show details of a given post
  • /blog/new => BlogController/New() => start writing a new post
  • /blog/delete/123 => BlogController/Delete(id = 123) => delete given post

这篇关于ASP.NET MVC 3.0路由行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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