2012-11-22 49 views
3

我有路由问题。我的网站上有很多页面是从数据库动态生成的。如何在mvc3 web应用程序中设置特定路由

,我要完成的第一件事是路由到这些网页那样:

“如何修理汽车”

www.EXAMPLE.com/How-to-repair-a-car

现在它的工作原理类似:www.EXAMPLE.com/Home/Index/How-to-repair-a-car

其次我的默认页必须是这样的:www.EXAMPLE.com

在开始页面将与pagging消息,所以如果有人在“第2页”按钮,单击例如,地址应看:www.EXAMPLE.com/page = 2

结论:

  1. 默认页面 - > www.EXAMPLE.com(含页面= 0)
  2. 默认页面与特定新闻页面 - > www.EXAMPLE.com/page=12
  3. 文章页面 - > www.EXAMPLE.com/如何修理汽车(没有参数'页面')路由sholud指向文章或错误404

PS:对不起,我的英语

+0

我不明白你的问题是什么?请张贴您的路由代码 – Curt

回答

1

尝试在路由设置来创建文章路线,像一个基本的路由实例这样的:

路由配置:

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

      routes.MapRoute(null, "{article}", 
          new {controller = "Home", action = "Article" }); 
      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 

的HomeController:

public class HomeController : Controller 
    { 
     public ActionResult Index(int? page) 
     { 
      var definedPage = page ?? 0; 
      ViewBag.page = "your page is " + definedPage; 
      return View(); 
     } 

     public ActionResult Article(string article) 
     { 
      ViewBag.article = article; 
      return View(); 
     } 
    } 

/页= 10 - ?工作

/如何对修理车 - 工程

这种做法优秀作品。

+0

非常感谢,这正是我想达到的。我还有一个小问题。我有什么改变/?page = 10 to: www.EXAMPLE.com/p?page=1 – Ellbar

+1

什么意思'p'(www.EXAMPLE.com/ ** p **?page = 1)in那种情况,这是行动吗? – testCoder

+0

对不起,我的错只是在你的回答中,谢谢:) – Ellbar

0

这里是www.example.com/How-to-repair-car

using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 

namespace Tipser.Web 
{ 
    public class MyMvcApplication : HttpApplication 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.MapRoute(
       "ArticleRoute", 
       "{articleName}", 
       new { Controller = "Home", Action = "Index", articleName = UrlParameter.Optional }, 
       new { userFriendlyURL = new ArticleConstraint() } 
       ); 
     } 

     public class ArticleConstraint : IRouteConstraint 
     { 
      public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
      { 
       var articleName = values["articleName"] as string; 
       //determine if there is a valid article 
       if (there_is_there_any_article_matching(articleName)) 
        return true; 
       return false; 
      } 
     } 
    } 
} 
相关问题