【操作记录】Asp.Net Core 的一些基本操作或属性

用于记录在项目中使用到的方法、属性、操作,持续更新中

.net core 开源地址

获取项目路径

var currentPath= string.Format("{0}App_Data", AppContext.BaseDirectory);
var path = Directory.GetCurrentDirectory();//获取当前的项目文件所在的目录。当使用命令启动时为执行dotnet命令所在的目录

获取到的路径如下:

  图片上传:

public async Task<IActionResult> Upload([FromServices]IHostingEnvironment environment)
{
      var result = new BaseResult();
      string path = string.Empty;
      var files = Request.Form.Files;
      if (files == null || files.Count() <= 0) {
           result.Msg = "请选择上传的文件。";
           return Json(result);
          }
      //格式限制
      var allowType = new string[] { "image/jpg", "image/png" , "image/jpeg" };
      if (files.Any(c => allowType.Contains(c.ContentType)))
      {
          string strpath = Path.Combine("images", DateTime.Now.ToString("MMddHHmmss"));
          path = Path.Combine(environment.WebRootPath, strpath);
          using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
          {
               await files[0].CopyToAsync(stream);
          }
             result.Data = strpath;
       }
       else
       {
          result.Msg = "图片格式错误";
       }
   return Json(result);
}

 ps:获取上传文件信息 可使用  IFormFileCollection 或者 Request.Form.Files来获取。

.net core 2.0发布后,不把 view 文件编译打包,修改 csproj文件中 PropertyGroup 节点,配置节MvcRazorCompileOnPublish设为false就行

 <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
  </PropertyGroup>

发布如图所示

 Drawing绘制图片,官方包:

System.Drawing.Common

静态文件的使用

在项目中静态文件的使用需要在Startup中的Configure方法中增加:

//使用静态文件
app.UseStaticFiles();

这样就可以访问所有wwwroot目录下的静态文件,但是若想访问Views/Menu/Index.js文件,还需要在Configure方法中增加:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory())
});
//在页面的引用方式
@section scripts{
    <script src="~/Views/Department/Index.js"></script>
}

在mvc中加载其他页面到当前页面:

@Html.Partial("_Edit")

@RenderSection 详解:http://www.cnblogs.com/Joetao/articles/4191682.html

session过期验证:

 /// <summary>
    /// 拦截控制器
    /// </summary>
    public class InterceptController : Controller
    {
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            byte[] result;
            //获取session的值
            context.HttpContext.Session.TryGetValue("UserInfo", out result);
            if (result==null)
            {
                //重定向到登录页面
                context.Result = new RedirectResult("/Login");
            }
            base.OnActionExecuted(context);
        }
    }

linux System.Drawing.Bitmap 报错解决办法

#locate libdl
#cd /usr/lib64 #ln -s libdl-2.17.so libdl.so

如果 locate libdl 报以下错误:
-bash: locate: command not found

其原因是没有安装mlocate这个包

安装一下包:#yum  -y install mlocate

再更新一下库:#updatedb

最后最关键的是安装一下 libgdiplus 库,搞定!

yum install libgdiplus-devel

模型校验

使用ModelState.IsValid 属性会检查验证是否失败或成

 [Display(Name "电子邮件")]字段设置显示名称

 

posted @ 2016-11-28 15:52  ice.ko  阅读(765)  评论(0编辑  收藏  举报