mvc网站迁移.net core记录

时间:2020-12-06 06:34:15

接口return Json()时序列号小写的问题

在Startup.cs-》ConfigureServices方法配置一下解决

        public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
.AddJsonOptions(op => op.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.DefaultContractResolver());
}

视图中输出中文会乱码

mvc网站迁移.net core记录
ConfigureServices方法中配置即可,详情见院长文章 http://www.cnblogs.com/dudu/p/5879913.html

            services.Configure<WebEncoderOptions>(options =>
{
options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});

.net core中配置伪静态

Configure方法中,还是一样的配方
mvc网站迁移.net core记录

         app.UseMvc(routes =>
{
routes.MapRoute(
name: "index",
template: "index.html",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "detail",
template: "detail/{id}.html",
defaults: new { controller = "Home", action = "Detail" }
);
routes.MapRoute(
name: "add",
template: "add.html",
defaults: new { controller = "Home", action = "Add" }
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

单个文件上传

        [HttpPost]
public IActionResult Upload(IFormFile file)
{
string previewPath = "";//加域名什么的
long size = 0;
var upFileName = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
var fileName = Guid.NewGuid() + Path.GetExtension(upFileName);
size += file.Length;
if (size > UploadMaxLength)
{
return Json(new
{
code = 0,
msg = "图片太大,不能超过5M"
});
}
previewPath += "/uploads/" + fileName;
var savePath = _hostingEnv.WebRootPath + @"\uploads\" + fileName;
var saveDir = _hostingEnv.WebRootPath + @"\uploads\";

if (!Directory.Exists(saveDir))
{
Directory.CreateDirectory(saveDir);
}
using (FileStream fs = System.IO.File.Create(savePath))
{
file.CopyTo(fs);
fs.Flush();
}
return Json(new
{
code = 0,
msg = "上传成功",
data = new
{
src = previewPath,
title = ""
}
});
}