一步步教你在Asp.net Mvc中使用UEditor编辑器

时间:2021-05-25

最近在配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法。

首先去这里下载

然后重名一下下载的文件夹为ueditor(想自己起名字也可以,不过配置文件就要改成一样的了,这里为了避免麻烦,就跟配置文件里的名字保持一致了)

然后根据自己需要放到项目中,我是这么放的

上面建的文件夹也是跟配置文件保持一致,并不是非要照着这么做,这里只是跟配置文件保持一致,可以根据自己需要修改配置文件,自己想怎么建都行,看看配置文件就明白了

config.json

{ "imageActionName": "uploadimage", "imageFieldName": "upfile", "imageMaxSize": 2048000, "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "imageCompressEnable": true, "imageCompressBorder": 1600, "imageInsertAlign": "none", "imageUrlPrefix": "Scripts/ueditor/net/", "imagePathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 非法字符 \ : * ? " < > | */ "scrawlActionName": "uploadscrawl", "scrawlFieldName": "upfile", "scrawlPathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "scrawlMaxSize": 2048000, "scrawlUrlPrefix": "/ueditor/net/", "scrawlInsertAlign": "none", "snapscreenActionName": "uploadimage", "snapscreenPathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "snapscreenUrlPrefix": "/ueditor/net/", "snapscreenInsertAlign": "none", "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherActionName": "catchimage", "catcherFieldName": "source", "catcherPathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "catcherUrlPrefix": "/ueditor/net/", "catcherMaxSize": 2048000, "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "videoActionName": "uploadvideo", "videoFieldName": "upfile", "videoPathFormat": "upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", "videoUrlPrefix": "/ueditor/net/", "videoMaxSize": 102400000, "videoAllowFiles": [ ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], "fileActionName": "uploadfile", "fileFieldName": "upfile", "filePathFormat": "upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", "fileUrlPrefix": "/ueditor/net/", "fileMaxSize": 51200000, "fileAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ], "imageManagerActionName": "listimage", "imageManagerListPath": "upload/image", "imageManagerListSize": 20, "imageManagerUrlPrefix": "/ueditor/net/", "imageManagerInsertAlign": "none", "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "fileManagerActionName": "listfile", "fileManagerListPath": "upload/file", "fileManagerUrlPrefix": "/ueditor/net/", "fileManagerListSize": 20, "fileManagerAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ] }

如果要自定义,下面的配置项必须改

"imageUrlPrefix": "Scripts/ueditor/net/", "imagePathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "fileUrlPrefix": "/ueditor/net/",

配置文件 ueditor.config.js 下面的

window.UEDITOR_HOME_URL 可以不指定也可以指定,不指定的话,那就在MVC视图里面指定,如下所示

......<script type="text/javascript"> var editor = new baidu.editor.ui.Editor({ UEDITOR_HOME_URL: '/Scripts/ueditor/',//配置编辑器路径 iframeCssUrl: '/Scripts/ueditor/themes/iframe.css',//样式路径......</script>......

总之就是下面几个步骤

1.下载UEditor插件,把文件夹放在项目根目录下,改名,省事的话就跟配置文件保持一致。

2.建立一个Controller, Action方法加上 [ValidateInput(false)]

public class UEditorController : Controller { [ValidateInput(false)] // GET: UEditor public ActionResult Index(FormCollection fc) { } }

3.建立视图,视图名称跟Controller的Action方法名称一致

<script src="~/Scripts/ueditor/ueditor.config.js"></script><script src="~/Scripts/ueditor/ueditor.all.min.js"></script><link href="~/Scripts/ueditor/themes/iframe.css" rel="stylesheet" /><script src="~/Scripts/ueditor/lang/zh-cn/zh-cn.js"></script>@{ ViewBag.Title = "UEditorDemo";}<script type="text/javascript"> var editor = new baidu.editor.ui.Editor({ UEDITOR_HOME_URL: '/Scripts/ueditor/',//配置编辑器路径 iframeCssUrl: '/Scripts/ueditor/themes/iframe.css',//样式路径 initialContent: '',//初始化编辑器内容 autoHeightEnabled: true,//高度自动增长 minFrameHeight: 500,//最小高度 autoFloatEnabled: true, initialFrameWidth: 690, initialFrameHeight:483 }); editor.render('editor');</script>@using (Html.BeginForm("Index", "UEditor", FormMethod.Post)){ <textarea id="editor" name="editor"></textarea> <input type="submit" value="提交" />}

4.修改Controller如下

public ActionResult Index(FormCollection fc) { var content = fc["editor"]; return View(); }

5.首页弹出UEditor的页面用来测试,修改App_Start文件夹下的RouteConfig

public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", //defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } defaults: new { controller = "UEditor", action = "Index", id = UrlParameter.Optional } ); } }

6.默认我们并不需要UEditor工具栏上的所有按钮,需要屏蔽一些,这个时候需要修改ueditor.config.js

, toolbars: [[ //'fullscreen', 'source', '|', 'undo', 'redo', '|', //'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', //'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', //'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', //'directionalityltr', 'directionalityrtl', 'indent', '|', //'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|', //'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|', //'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|', //'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|', //'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|', //'print', 'preview', 'searchreplace', 'drafts', 'help' 'undo', 'redo', '|', 'bold', 'forecolor', 'removeformat', 'autotypeset', 'pasteplain', '|', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'link', 'unlink', '|', 'simpleupload', 'insertimage', '|', 'wordimage', '|', 'inserttable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'splittocells' ]]

如上,我把原始的工具栏配置项注释掉了,运行一下出现如下效果


已经少了不少平常用不上的按钮,好了大功告成~!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用Asp.net能带来一定的帮助,如果有疑问大家可以留言交流。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章