• 把旧系统迁移到.Net Core 2.0 日记(6) MapRoute/Area/ViewPath


    我想实现 http://localhost:5000/{moduleName}/{controller}/{action}/{id?} 这样的url.

    有2个方法

    方法1: 在路由里设置多个module

                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "CRM",
                        template: "CRM/{controller=Home}/{action=Index}/{id?}",
                        defaults:null,
                        constraints:null,
                        dataTokens:new string[] { "FoxCRMCore.Controllers.CRM" });
                    
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

    MVC return View(string viewName) 中viewName的表达方式

    return View("../CRM/Announcement/Index");  
    //或者
    return View("/Views/CRM/Announcement/Index.cshtml");   

    当然你不想这样Hardcode View的路径. 可以在ConfigService方法里加入自定义ViewLocationFormats

                services.Configure<RazorViewEngineOptions>(Options =>
                {
                    Options.ViewLocationFormats.Add("/Views/CRM/{1}/{0}.cshtml");
                });

     

    参考这个

    https://www.cnblogs.com/dudu/archive/2012/12/14/asp-net-mvc-area.html

    http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_4_6-areas.html

    但是第1种写法自定义RazorViewEngineOptions还是不够优雅, 因为可能有10个模块,你就要写10条map-Route,10条RazorViewEngineOptions

    这个时候,可以用Area, 方法2:

    在StartUp.cs的 ConfigureServices方法里加入:

                //{2}=AreaName,{1}=Controller,{0}=Action
                services.Configure<RazorViewEngineOptions>(options =>
                {
                    options.AreaViewLocationFormats.Add("/Views/{2}/{1}/{0}.cshtml");
                });

    Configure方法里改成

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

    然后在controller里 加入AreaAttribute

        [Area("CRM")]
        public class AnnouncementController : Controller
       {}

    这样就能无需指定View的路径了.

    return View(); 

    netCore Controller返回的JsonResult ,即使你C#代码里首字母是大写, 也会默认是CamelCase, 首字母小写的,

    参考这个: https://github.com/aspnet/Mvc/issues/4283

                var list = _context.Announcements
                    .Select(entity => new
                    {
                        entity.ID,
                        entity.Subject,
                        entity.ContentDesc,
                        CreateDate = entity.CDate.ToString(),
                        entity.CUser,
                        entity.IsActive
                    });
    
                var result = new { total = list.Count(), rows = list.ToList() };
    
                return Json(result);

    返回的json结果

    {
        "total": 1,
        "rows": [
        {
        "id": 1,
        "subject": "test 公告",
        "contentDesc": "公告内容",
        "createDate": "2018-04-10 21:41:47",
        "cUser": 1,
        "isActive": true
        }
        ]
    }

    吐槽一下Mac 的VS2017, 我在View里更改html,加入JEasyUI的datagrid,怎么都不生效,重新build & Run 几次之后又生效了. 难道是View的缓存?

    View的更改也要重新Build???

  • 相关阅读:
    Effective java 读书笔记
    python测试api接口
    Git 提交后开始自动构建
    修改docker的默认存储位置
    golang实现ios推送
    NSRangeFromString 测试
    Container View Controller
    ios自定义View自动布局时计算大小
    Java执行groovy脚本
    gradle使用eclipse debug 代码
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/net_core_daily_6.html
Copyright © 2020-2023  润新知