【操作记录】Asp.Net Core 的一些基本操作或属性
2024-10-18 19:29:29
用于记录在项目中使用到的方法、属性、操作,持续更新中
.net core 开源地址
图片上传:
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() <= ) {
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[].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);
}
}
最新文章
- 设计模式之迪米特原则(LOD)(最少知识原则)
- 使用WebRTC搭建前端视频聊天室——点对点通信篇
- [知识整理]Java集合
- java 中遍历hashmap 和hashset 的方法
- python tornado框架使用
- IIS开启伪静态后html静态页面无法访问的解决方法
- 移动端 iphone touchmove滑到边界(浏览器地址拦及以上) touchend失效解决办法
- 如何使用kali的Searchsploit查找软件漏洞
- 车牌识别OCR—易泊时代智慧城市解决方案模块
- 进击Node.js基础(一)
- 【Canal源码分析】parser工作过程
- Mysql 上亿级数据导入Hive思路分享
- tp5 删除服务器文件
- SQL Server 2012-2016-2017 简体中文版下载和序列号
- socket编程及API简介
- inline详解
- Criteria查询
- 图文例解C++类的多重继承与虚拟继承
- P4 PI库安装
- Totem协议(SRP/RRP)讲解
热门文章
- javascript修改div大小遮挡页面渲染问题
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (六) 树莓派查询子节点温湿度数据
- 最新版ABP 动态WebAPI 日期转json带T的解决方案| ABP DateTIme Json format
- vue开发完成后打包后图片路径不对
- SQL DATACOMPARE 实现两个数据库的同步处理.
- [转帖]Cookies和Session的区别和理解
- centos 升级内核(编译安装)
- elasticsearch6 学习之安装
- Embarcadero Delphi 7 Enterprise 7.0.4.453 中文版
- java使用Cookie判断用户登录情况