我想实现 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. 【BZOJ】1831: [AHOI2008]逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1831 考虑$-1$的位置上填写的数字一定是不降的. 令${f[i][j]}$表示$DP$到 ...

  2. jsp导入数据库数据写法(模板)

    1.导入表格模板 <%@ page language="java" contentType="text/html; charset=utf-8" page ...

  3. nodejs + ts 配置

    参考:https://github.com/nestjs/typescript-starter 和 How to get auto restart and breakpoint support wit ...

  4. RestTemplate学习

    在学习spring cloud的时候,用到了RestTemplate,找到一篇博客,写的很好,学习转载! 文章转载自:https://blog.csdn.net/itguangit/article/d ...

  5. VC工程编译相关

    ①error C4996: 'sprintf': This function or variable may be unsafe 这不是语法的错误,而是IDE默认禁止这种容易产生漏洞的旧函数,解决的方 ...

  6. Http File Server小工具

    一般情况下,在做一些测试(比如下载服务)的时候需要提供一个http文件下载服务. 下面这个轻量级的工具HFS可以在本地提供http服务: 官网地址传送门:Http File Server

  7. 分享:selenium(一) xpath

    xpath无所不能定位.   https://www.w3.org/TR/xpath/all/#axes 两个神器:firebug.xpath-checker 举例:混合定位 //td[a//fron ...

  8. 单细胞RNA-seq比对定量用什么工具好?使用哪个版本的基因组?数据来说话

    这么多工具和基因组版本,选择困难症犯了,到底用哪个好呢? 2018 nature - Developmental diversification of cortical inhibitory inte ...

  9. tornado web

    tornado web frame: 非阻塞服务器,速度快,运用epoll 模板语言+render(),实现根据用户输入,自动渲染页面的动态效果. 在使用模板前需要在setting中设置模板路径: s ...

  10. English trip M1 - AC9 Nosey people 爱管闲事的人 Teacher:Solo

    In this lesson you will learn to talk about what happened. 在本课中,您将学习如何谈论发生的事情. 课上内容(Lesson) # four “ ...