受欢迎的博客标签

1.add   custom router in infrastructure\genericurlrouteprovider.cs

public partial class GenericUrlRouteProvider : IRouteProvider

{

public void RegisterRoutes(IRouteBuilder routeBuilder) {

//and default one routeBuilder.MapRoute("Default", "{controller}/{action}/{id?}");

#region custom extend by www.iaspnetcore.com

routeBuilder.MapRoute("CustomBlogPost", "blogpost/{blogPostId?}/{SeName?}", new { controller = "Blog", action = "BlogPost" });

routeBuilder.MapRoute("CustomBlogOriginWithUserName", "{UserName}/blog/blogpost/{blogPostId?}/{SeName?}", new { controller = "Blog", action = "BlogPost" }); .... }

2.must before old router, Override it routeBuilder.

MapLocalizedRoute("BlogPost", "{SeName}", new { controller = "Blog", action = "BlogPost" });.

 

Nop.Web.Framework\Mvc\Routes\RegisterRoutes.cs

 //news
            routes.MapRoute("NewsItem", "News/NewsItem/{Id?}/{SeName?}", new { controller = "News", action = "NewsItem" });

cshtml

 <div class="news-items row">
            @foreach (var item in Model)
            {
                <div class="news-item col-lg-4">
                    <div class="news-head">
                        <a class="news-title" href="@Url.RouteUrl("NewsItem", new {id=item.Id, SeName = item.SeName })">@item.Title</a>
                        <span class="news-date">-@item.CreatedOnUtc.ToString("D")</span>
                    </div>
                    <div class="news-body">
                        @Html.Raw(item.Short)
                    </div>
                    <div class="buttons">
                        <a href="@Url.RouteUrl("NewsItem", new {id=item.Id, SeName = item.SeName })" class="button-2 btn btn-primary read-more">@T["News.MoreInfo"]</a>
                        
                    </div>
                </div>
            }
        </div>

 

2. how to add custom static route table

step 1:write a static route table

\Nop.Web.Framework\Mvc\Routes\RegisterRoutes.cs

using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;


namespace Microsoft.AspNetCore.Builder
{
    public static class RegisterRoutes
    {

        /// <summary>
        /// 注册静态路由
        /// </summary>
        /// <param name="routes"></param>
        public static void MapModuleRoute(this IRouteBuilder routes)
        {
            //reorder routes so the most used ones are on top. It can improve performance

            //custom article RSS
            routes.MapRoute("ArticleRSS", "article/rss/{languageId:min(0)}",
                new { controller = "Article", action = "ListRss" });
            ...

step 2: register in startup.cs

 app.UseMvc(routes =>
            {
                routes.MapRoute(
                name: "areas",
                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
              );

                //定义一张路由表
                routes.MapModuleRoute();

 

3. custom RouteProvider

step 1:write a custom RouteProvider

using System.Threading.Tasks;

using Microsoft.AspNetCore.Routing;

using Microsoft.Extensions.DependencyInjection;

namespace Nop.Web.Framework.Mvc.Routes
{
    public class RouteProvider : IRouter
    {
        private readonly IRouter target;

        public RouteProvider(IRouter target)
        {
            this.target = target;
        }

        public async Task RouteAsync(RouteContext context)
        {

            //example http://www.iaspnetcore.com/about

            string domain = context.HttpContext.Request.Host.Host;//获取当前请求域名

           

            var requestPath = context.HttpContext.Request.Path.Value; //here requestPath="/about"

            if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
            {
                // Trim the leading slash
                requestPath = requestPath.Substring(1);  //here requestPath="about"
            }

            if (requestPath == "/About")
            {

                var oldRouteData = context.RouteData;
                var newRouteData = new RouteData(oldRouteData);
                newRouteData.Routers.Add(target);

                newRouteData.Values["controller"] = "Article";
                newRouteData.Values["action"] = "Detail";
                newRouteData.Values["id"] = "586faeaf0141bf20236530f0";

                context.RouteData = newRouteData;
                await target.RouteAsync(context);

                return;
            }

 

step 2: add custom routerPovider in startup.cs

 app.UseMvc(routes =>
            {
                routes.MapRoute(
                name: "areas",
                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
              );

                //定义一张路由表
                routes.MapModuleRoute();

                //自定义路由处理
                routes.Routes.Add(new RouteProvider(routes.DefaultHandler));