Cube/NewLife.CubeNC
大石头 4b7fa28f08 升级代码生成器 2024-07-14 19:37:54 +08:00
..
Areas 手机端 2024-07-12 02:35:54 +08:00
Charts 支持多个Y轴索引。设置1表示使用第二个Y轴 2024-04-29 17:26:52 +08:00
Common 恢复数据后,刷新页面 2024-06-03 16:31:03 +08:00
Controllers [feat]新增ForceRedirect,强制跳转。指定目标schema和host,在GET访问发现不一致时强制跳转,host支持*。常用于强制跳转https,如https://*:8081 2024-04-18 18:05:41 +08:00
Extensions [feat]新增ForceRedirect,强制跳转。指定目标schema和host,在GET访问发现不一致时强制跳转,host支持*。常用于强制跳转https,如https://*:8081 2024-04-18 18:05:41 +08:00
Jobs CronJob特性增加Enable,创建作业时是否自动启用 2024-02-29 21:14:39 +08:00
Membership [feat]菜单特性新增HelpUrl指向帮助文档,借助Menu表Ex4字段。 2024-04-04 15:41:49 +08:00
Modules 升级代码生成器 2024-07-14 19:37:54 +08:00
Properties 升级代码生成器 2024-07-14 19:37:54 +08:00
Results Csv导出枚举类型时,使用数字而不是字符串 2024-04-29 09:54:28 +08:00
Services 提前注入ICacheProvider,避免后续异常 2024-07-10 13:53:16 +08:00
Session [fix]修正普通可空字段遇到并行冲突问题,改为并行字段 2023-12-06 23:23:18 +08:00
ViewModels [feat]ListField新增GetClass委托,方便根据实体对象控制单元格样式;列表字段集合新增GetRowClass委托,方便根据实体对象控制整行样式; 2024-06-24 19:00:19 +08:00
Views 增加移动端页面 2024-07-11 04:46:06 +08:00
WebMiddleware v6.1.2024.0711 2024-07-11 00:31:13 +08:00
wwwroot 使用地区控件时,也要自动初始化地区数据 2024-02-29 21:07:06 +08:00
CubeService.cs IP访问太快,限流测试通过 2024-05-23 23:19:30 +08:00
NewLife.CubeNC.csproj [fix]修正Json.Writer.CamelCase配置无效的BUG,该BUG导致魔方Json返回无法序列化为驼峰命名,前端匹配失败。 2024-07-13 12:24:33 +08:00
README.md 实体控制器支持指定为另一个区域,从而把菜单合并到其下。搜索所有控制器,找到本区域所属控制器,优先特性其次命名空间 2024-01-17 22:54:30 +08:00
Setting.cs 恢复数据后,刷新页面 2024-06-03 16:31:03 +08:00

README.md

asp.net core移植

[TOC]

替换方案

Asp. Net Mvc ASP. NET Core 说明
HttpRuntime.AppDomainAppVirtualPath 据说已被干掉,用不到
HttpRuntime.AppDomainAppPath (IHostingEnvironment)Env.ContentRootPath
HttpPostedFileBase IFormFile
Request[key] Request.Form[key] & Request.Query[key] 需要判断这两个返回的类型StringValues而不会是null
Request.IsAjaxRequest() Request.Headers["x-requested-with"]=="XMLHttpRequest"
Request.QueryString[key] Request.Query[key]
Request.RawUrl Request.GetEncodedUrl()
Request.RouteData.GetRequiredString() HttpContext.GetRouteValue()
Request.ServerVariables Request.Headers ASP.NET Core中没有ServerVariables的对应实现有的可以在HttpContext.Request.Headers中获取
Request.Url.PathAndQuery Request.GetEncodedPathAndQuery()
Request.UrlReferrer Request.Headers[HeaderNames.Referer]
Request.UserAgent Request.Headers[HeaderNames.UserAgent]
Response.Output new StreamWriter(HttpContext.Response.Body)
System.Runtime.Caching Microsoft.Extensions.Caching.Memory

注意

  • request.Form根据请求类型决定该参数是否可以正常读取

路由

  • 其中url以及以前defaults参数的默认值可使用template代替直接指定默认值
  • id后一定要加问号效果等同于Asp. Net Mvcid = UrlParameter.Optional
 app.UseMvc(routes =>
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
          });

区域

  • 官方文档
  • Asp. Net Mvc中的区域会自动注册本区域文件夹里面的控制器作为区域的控制器, Asp. Net Core Mvc需要在控制器使用特性[AdminArea]指定区域,如果不指定区域就是和正常控制器一样,即使它位于区域文件夹
  • Asp. Net Core Mvc一定要进行区域路由注册,否则无法匹配带有Area特性的控制器
  • 在没有任何路由注册默认控制器为Index的情况下,如果有控制器名为IndexController,在Asp. Net Mvc中访问/Admin/,会匹配此控制器,但在Asp. Net Core Mvc中需要指定路由默认控制器有Index才能匹配
  • 总的来说,Asp. Net Core Mvc什么都要指定都要设置,不能偷懒
  • 路由注册示例
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            "CubeAreas",
            "{area=Admin}/{controller=Index}/{action=Index}/{id?}");
        endpoints.MapRazorPages();
    });
    .Build();

视图命名空间导入

  • web.config换成_ViewImports.cshtml,使用@using

http模块到中间件

添加http上下文

  • ConfigureServices
//添加Http上下文访问器
StaticHttpContextExtensions.AddHttpContextAccessor(services);
  • Configure
//配置静态Http上下文访问器
app.UseStaticHttpContext();

Razor视图

  • 参考博客
  • 分部页替换:<partial name="_Login_Login"/>@await Html.PartialAsync("_Login_Login")

导入命名空间

  • Views文件夹下的_ViewImports.cshtml

RazorOptions

  services
      .AddMvc()
      .AddRazorOptions(opt =>
      {
          opt.ViewLocationFormats.Clear();
          opt.AreaViewLocationFormats.Clear();
          opt.ViewLocationFormats.Add("~/Views/{1}/{0}.cshtml");
          opt.ViewLocationFormats.Add("~/Views/Shared/{0}.cshtml");
          opt.AreaViewLocationFormats.Add("~/Areas/{2}/Views/{1}/{0}.cshtml");
          opt.AreaViewLocationFormats.Add("~/Areas/{2}/Views/Shared/{0}.cshtml");
      });

视图引擎

模型绑定

IModelBinder

  • 官网
  • 继承自Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder
  • CreateModel改为BindModelAsync方法
  • modelType模型类型bindingContext.ModelType
  • controllerContextbindingContext.ActionContext
  • 返回object改成
  bindingContext.Result = ModelBindingResult.Success(entity);
  return Task.CompletedTask;
  • 示例
    public class EntityModelBinder:IModelBinder
    {
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var modelType = bindingContext.ModelType;
            var controllerContext = bindingContext.ActionContext;
            if (modelType.As<IEntity>())
            {
                var fact = EntityFactory.CreateFactory(modelType);
                if (fact != null)
                {
                    bindingContext.Result = ModelBindingResult.Success(fact.Create());
                }
            }
            return Task.CompletedTask;
        }

IModelBinderProvider

    public class EntityModelBinderProvider : IModelBinderProvider
    {
        public IModelBinder GetBinder(ModelBinderProviderContext context) => 
        context.Metadata.ModelType.As<IEntity>() ? new EntityModelBinder() : null;
    }

使用

  • 放在第一位优先调用
    services.AddMvc(opt =>
    {
        //模型绑定
        opt.ModelBinderProviders.Insert(0,new EntityModelBinderProvider());
    });

过滤器

上传文件大小限制

.UseKestrel(options =>
{
    options.Limits.MaxRequestBodySize = null;
}

数据验证

  • ValidateAntiForgeryToken

模型绑定验证

响应流写入、设置、推送

登录授权