我想实现 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???

把旧系统迁移到.Net Core 2.0 日记(6) MapRoute/Area/ViewPath的更多相关文章

  1. 把旧系统迁移到.Net Core 2.0 日记 (15) --Session 改用Redis

    安装Microsoft.Extensions.Caching.Redis.Core NuGet中搜索Microsoft.Extensions.Caching.Redis.Core并安装,此NuGet包 ...

  2. 把旧系统迁移到.Net Core 2.0 日记(1) - Startup.cs 解析

    因为自己到开发电脑转到Mac Air,之前的Webform/MVC应用在Mac 跑不起来,而且.Net Core 2.0 已经比较稳定了. 1. 为什么会有跨平台的.Net Core  近年来,我们已 ...

  3. 把旧系统迁移到.Net Core 2.0 日记 (12) --发布遇到的问题

    1. 开发时是在Mac+MySql, 尝试发布时是在SQL2005+Win 2008 (第一版) 在Startup.cs里,数据库连接要改,分页时netcore默认是用offset关键字分页, 如果用 ...

  4. 把旧系统迁移到.Net Core 2.0 日记 (17) --多租户和SoftDelete

    在EF Core 2.0版本中出现了全局过滤新特性即HasQueryFilter,它出现的意义在哪里?能够解决什么问题呢? 通过HasQueryFilter方法来创建过滤器能够允许我们对访问特定数据库 ...

  5. 把旧系统迁移到.Net Core 2.0 日记(10) -- EF core 和之前版本多对多映射区别

    EF Core 现在不支持多对多映射,只能做2个一对多映射. 比如Product和Category 我现在定义Product和Category是多对多关系. 那么实体定义如下: public clas ...

  6. 把旧系统迁移到.Net Core 2.0 日记 (18) --JWT 认证(Json Web Token)

    我们最常用的认证系统是Cookie认证,通常用一般需要人工登录的系统,用户访问授权范围的url时,会自动Redirect到Account/Login,登录后把认证结果存在cookie里. 系统只要找到 ...

  7. 把旧系统迁移到.Net Core 2.0 日记(8) - EASYUI datagrid+ Dapper+ 导出Excel

    迁移也没太大变化,有一个, 之前的Request.QueryString 是返回NameValueCollection, 现在则是返回整个字符串. 你要改成Request.Query[“key”] 直 ...

  8. 把旧系统迁移到.Net Core 2.0 日记(5) Razor/HtmlHelper/资源文件

    net core 的layout.cshtml文件有变化, 区分开发环境和非开发环境. 开发环境用的是非压缩的js和css, 正式环境用压缩的js和css <environment includ ...

  9. 把旧系统迁移到.Net Core 2.0 日记(4) - 使用EF+Mysql

    因为Mac 不能装SqlServer, 所以把数据库迁移到MySql,然后EntityFramework要改成Pomelo.EntityFrameworkCore.MySql 数据库迁移时,nvarc ...

  10. 把旧系统迁移到.Net Core 2.0 日记(3) - 详解依赖注入 (转)

    关于DI 依赖注入, 转载这篇文章, 写得很好的. ----------------------------- DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关 ...

随机推荐

  1. spoj IITWPC4F - Gopu and the Grid Problem 线段树

    IITWPC4F - Gopu and the Grid Problem no tags  Gopu is interested in the integer co-ordinates of the ...

  2. 2.0 vue内置指令与自定义指令

    1.1 常用内置指令 1) v:text : 更新元素的 textContent 2) v-html : 更新元素的 innerHTML 3) v-if : 如果为 true, 当前标签才会输出到页 ...

  3. VC.窗口最前(置顶)

    1.Delphi7的代码 procedure TfrmMain.cbWndTopmostClick(Sender: TObject); var liExStyle :LongInt; begin // ...

  4. [转][cesium]1.添加本地服务器

    转自:http://www.cnblogs.com/fuckgiser/p/5633748.html 此系列cesium总教程:  https://www.cnblogs.com/fuckgiser/ ...

  5. Grunt、Gulp区别 webpack、 requirejs区别

    1. 书写方式 grunt 运用配置的思想来写打包脚本,一切皆配置,所以会出现比较多的配置项,诸如option,src,dest等等.而且不同的插件可能会有自己扩展字段,导致认知成本的提高,运用的时候 ...

  6. python模块(4)

    re正则 re.match 从头开始匹配 re.search 匹配包含 re.findall 把所有匹配到的字符放到以列表中的元素返回(没有group()方法) re.splitall 以匹配到的字符 ...

  7. Python中什么是变量

    在Python中,变量的概念基本上和初中代数的方程变量是一致的. 例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是4,当x=5时,计算结果是25. 只是在计算机程序中,变量不仅可以是 ...

  8. MySQL学习(六)

    1 注意 select cout(*) from 表名: 查询的就是绝对的行数,哪怕某一列所有字段全部为NULL,也计算在内.而select cout(列名) form 表名:查询的是该列不为null ...

  9. 优雅地记录Python程序日志1:logging模块简介

    本文摘自:https://zhuanlan.zhihu.com/p/31893724 本篇涉及: logging模块的调用: 保存log日志为文件: 调整输入日志等级: 修改日志消息格式: 前言 在使 ...

  10. spring集成JMS访问ActiveMQ

    首先我们搭建一个spring-mvc项目,项目可以参考:spring-mvc 学习笔记 步骤: 在pom.xml中加上需要的包 修改web.xml,增加IOC容器 spring配置文件applicat ...