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

"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方法名称一致

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

<script src="https://www.jb51.net/~/Scripts/ueditor/ueditor.config.js"></script> <script src="https://www.jb51.net/~/Scripts/ueditor/ueditor.all.min.js"></script> <link href="https://www.jb51.net/~/Scripts/ueditor/themes/iframe.css" /> <script src="https://www.jb51.net/~/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></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 Mvc中使用UEditor编辑器


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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdppxs.html