加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

基于.NET Core 3.1 网站开发和部署的方法

发布时间:2020-09-15 17:48:49 所属栏目:Asp教程 来源:互联网
导读:这篇文章主要介绍了基于.NET Core 3.1 网站开发和部署的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面

List<Claim> claims = new List<Claim>() { new Claim("username", admin.LoginName) }; ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); AuthenticationProperties properties = new AuthenticationProperties() { IsPersistent = true }; await HttpContext.SignInAsync ( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), properties );

其他需登录后才能访问的资源,在控制器上添加 [Authorize]标记

[Authorize] public class HomeController : Controller { public IActionResult Index() { return View(); } public IActionResult Welcome() { ViewData["Message"] = "Your welcome message"; return View(); } }

3.使用ajax提交表单

在dotnet core 中不再支持@Ajax.Form方式,而是使用jquery插件的方式支持了。
通过定义data-* 属性来支持类似的功能

和Ajax助手的对照

====================================================== AjaxOptions HTML attribute ====================================================== Confirm data-ajax-confirm HttpMethod data-ajax-method InsertionMode data-ajax-mode LoadingElementDuration data-ajax-loading-duration LoadingElementId data-ajax-loading OnBegin data-ajax-begin OnComplete data-ajax-complete OnFailure data-ajax-failure OnSuccess data-ajax-success UpdateTargetId data-ajax-update Url data-ajax-url ======================================================

这个特性只能在form和a标签上起作用
使用方法:

①下载插件并引用到项目中
地址:https://github.com/aspnet/jquery-ajax-unobtrusive/releases
将src文件夹中的js文件拷贝到项目对应的存放位置

②编写需要的js函数
编写回调函数

<script> var onSuccess=function(data){ alert(data); $("#mainForm")[0].reset(); dishImg.src = "/images/default.png"; }; var onFailed=function(data){ alert(data); }; </script>

③使用data属性改写标签

这里注意:要使用ajax提交表单,data-ajax="true"必须要设置为true。
data-ajax-confirm=“确认要提交吗?” 这里是弹出框的内容,不是具体的函数名
data-ajax-begin
data-ajax-complete
data-ajax-failure
data-ajax-success
这些属性值就是回调函数的名称。

4.CKeditor使用

推荐使用ckeditor4,因为5版本中文输入有问题。
使用步骤:

下载编辑器的软件包

在页面中引入它的js脚本

<script src=""></script>

使用texterea作为目标
编辑器的高度可以config.js文件中设置

<textarea rows="20"></textarea>

在js中创建

<script> CKEDITOR.replace( 'editor' ); </script>

自定义配置
修改配置文件config.js,推荐直接在默认的文件中添加需要的配置。

CKEDITOR.editorConfig = function( config ) { config.language = 'es'; config.uiColor = '#F7B42C'; config.height = 300; config.toolbarCanCollapse = true; };

获取编辑器的内容
用于提交前验证是否有内容,NewsContent是编辑器textarea的id

var content=CKEDITOR.instances.NewsContent.getData();

注意此时数据验证通过,使用js提交表单的话,编辑器并没有替换原来的内容,需要手动替换
$("#NewsContent").html(content);
不然提交到控制器中是没有值的。

var onSubmit=function(){ var content=CKEDITOR.instances.NewsContent.getData(); if(content==""){ alert("新闻内容不能为空"); } else{ $("#NewsContent").html(content); $("#mainForm").submit(); } }

清空编辑器的内容
CKEDITOR.instances.NewsContent.setData("");

In rare cases it may happen that the server or application configuration
will reject submitted HTML content if it is not encoded first (e.g. ASP.NET ValidateRequest).
In such case check the config.htmlEncodeOutput option.

config.htmlEncodeOutput = true;

上传图片设置
需要外部插件:file browser,popup,filetools
配置config.js文件

config.filebrowserBrowseUrl = ‘/browser/browse.php'; config.filebrowserUploadUrl = ‘/uploader/upload.php';

控制器中的参数使用 (IFormFile upload) 使用的是upload的参数名。
如何给一个响应,放弃不用这个,使用filebrowserBrowseUrl这个功能,里面也有上传

具体方法是:

构建一个图片上传和浏览的页面

完成图片上传功能

图片选装功能

响应的js代码如下:

<script> // Helper function to get parameters from the query string. function getUrlParam( paramName ) { var reParam = new RegExp( '(?:[?&]|&)' + paramName + '=([^&]+)', 'i' ); var match = window.location.search.match( reParam ); return ( match && match.length > 1 ) ? match[1] : null; } var upload=function(){ var file=$(".btnUpload>input")[0].files[0]; if(file==null){ alert("请选择上传图片"); } else{ var formData=new FormData(); formData.append("upload",file); // 实例化一个AJAX对象 var xhr = new XMLHttpRequest(); xhr.onload = function() { $(".border3").first().before('<div><span><h3><span>new</span></h3><img src="/images/news/'+xhr.responseText+'"></span><div>'+xhr.responseText+'</div></div>'); } xhr.open("post",'@Url.Action("UploadImage","News")',true); // 发送表单数据 xhr.send(formData); } } var selectedImg=null; var select=function(img){ if(selectedImg!=null){ selectedImg.parents(".border3").removeClass("selected"); } selectedImg=$(img); selectedImg.parents(".border3").addClass("selected"); } var choose=function(){ if(selectedImg!=null){ var funcNum = getUrlParam( 'CKEditorFuncNum' ); var fileUrl = selectedImg.attr("src"); window.opener.CKEDITOR.tools.callFunction( funcNum, fileUrl ); window.close(); } else{ alert("请选装图片"); } } </script>

5.其他功能省略代码

六、使用依赖注入改进项目

1.抽取接口

通过vs来抽取接口,效率高。

2.重新组织项目结构

新增IDAL、IBLL、DBUtility
UI–>IBLL–>IDAL–>Models
BLL–>IBLL、IDAL
IDAL–>Models
DAL–>IDAL、DBUtility

3.注册依赖服务

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读