【问题标题】:How to set up specific routing in mvc3 web application如何在 mvc3 Web 应用程序中设置特定路由
【发布时间】:2012-11-10 17:34:36
【问题描述】:

我的路由有问题。我的网站上有许多从数据库动态生成的页面。

我想要完成的第一件事是像这样路由到这些页面:

“如何修理汽车”

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

目前它的工作方式如下:www.EXAMPLE.com/Home/Index/How-to-repair-a-car

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

在起始页上会有分页新闻,所以如果有人点击“page 2”按钮,地址应该是:www.EXAMPLE.com/page =2

结论:

  1. 默认页面 -> www.EXAMPLE.com(页面 = 0)
  2. 带有特定新闻页面的默认页面 -> www.EXAMPLE.com/page=12
  3. 文章页面 -> www.EXAMPLE.com/How-to-repair-car(无参数“页面”)路由应指向文章或错误404

PS:对不起我的英语

【问题讨论】:

  • 我不明白你的问题是什么?请发布您的路由代码

标签: c# asp.net-mvc-3 routing


【解决方案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 }
            );
        }
    }

家庭控制器:

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();
        }
    }

/?page=10 - 有效

/How-to-repair-car - 工作

这种方法非常有效。

【讨论】:

  • 非常感谢,这正是我想要实现的。我还有一个小问题。我要更改什么以将 /?page=10 更改为:www.EXAMPLE.com/p?page=1
  • 在这种情况下,'p'是什么意思(www.EXAMPLE.com/ p ?page=1),是行动吗?
  • 抱歉我的错误,我想要的只是你的回答,谢谢:)
【解决方案2】:

这是 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;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-02
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多