ASP.NET MVC 使用动态类型传递数据

问题起因 在 Controller 中定义动态类型的对象传递给视图,报错无法找到成员。 Controller: public ActionResult Index() { ViewBag.User = new { name = "她和她的猫", age = 20 }; return View(); } 视图: 姓名:@ViewBag.User.name 年龄:@ViewBag.User.age 按道理来说,这样写是没问题的,但是运行后却说Model中不存在‘name’。 ...

2017-04-09 · 2 分钟 · 766 字 · CSharp

ASP.NET 从客户端中检测到有潜在危险的 Request.Form 值

起因 今天在添加数据的时候系统抛出了下面这个错误,初步推断,应该是使用了ueditor编辑器传递的数据里面有未转义的html标签造成的。 System.Web.HttpRequestValidationException: 从客户端(details=&quot;&lt;h3&gt;关于我&lt;/h3&gt;&lt;p&gt;向支持者介...&quot;)中检测到有潜在危险的 Request.Form 值。 在 System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection) 在 System.Web.HttpRequest.<>c__DisplayClass5.<ValidateHttpValueCollection>b__3(String key, String value) 在 System.Web.HttpValueCollection.EnsureKeyValidated(String key) 在 System.Web.HttpValueCollection.Get(String name) 在 System.Collections.Specialized.NameValueCollection.get_Item(String name) 在ASP.NET中,每一次接收数据,都会对数据进行检查,如果数据中包含非法字符串,就会抛出这个错误。这个错误更像是一个提醒,提醒你这里的数据会含有非法字符串,会对系统造成威胁,让你采取一些措施进行防范。在刚学习 ASP.NET 的时候遇到过这个错误,后来使用 Server.HtmlEncode() 函数将所有的 HTML 标签进行转义才解决了这个问题。接收数据已经过滤转义了,但还是出现这个的错误,让我有点懵,最后经过不断的调试和百度才找到了解决方法。 ...

2017-02-08 · 2 分钟 · 707 字 · CSharp

贪心算法之字符串的完美度

最近在 51Nod 学习贪心算法入门,就把做题目的思路一些记录下来,欢迎指正。 题目详情我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同,而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度是一样的。 ...

一言不和就造轮子之 CookieHelper

最近开始学 ASP.NET,然后发现 Cookie 的设置不像 PHP 简单粗暴(PHP Cookie用法),于是就造个轮子省事,注释很详细,这里就不再赘言。 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Web;r namespace BuildWheel { /// <summary> /// Cookie辅助类 /// </summary> public class CookieHelper { /// <summary> /// 设置、删除、修改cookie /// </summary> /// <param name="cookieName">cookie名称</param> /// <param name="cookieValue">cookie值</param> /// <param name="expires">过期时间</param> public static void SetCookie(string cookieName, string cookieValue, DateTime expires) { HttpCookie cookie = new HttpCookie(cookieName) { Value = cookieValue, Expires = expires }; HttpContext.Current.Response.Cookies.Add(cookie); } /// <summary> /// 删除指定cookie /// </summary> /// <param name="cookieName">cookie名称</param> public static void DeleteCookie(string cookieName) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; if (cookie != null) { cookie.Expires = DateTime.Now.AddYears(-3); HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 清除所有cookie /// </summary> public static void ClearCookies() { HttpContext.Current.Request.Cookies.AllKeys.ToList().ForEach((e) => { HttpCookie cookie = HttpContext.Current.Response.Cookies[e]; cookie.Expires = DateTime.Now.AddYears(-3); HttpContext.Current.Response.Cookies.Add(cookie); }); } /// <summary> /// 获取cookie值 /// </summary> /// <param name="cookieName">cookie名称</param> /// <returns></returns> public static string GetCookieValue(string cookieName) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; string cookieValue = string.Empty; if (cookie != null) { cookieValue = cookie.Value; } return cookieValue; } } } 有错误欢迎指出~ ...

2016-12-06 · 2 分钟 · 552 字 · CSharp

ASP.NET 上传图片

前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPic.aspx.cs" Inherits="UploadPicDemo.UploadPic" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>ASP.NET上传图片</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Image ID="imagePic" runat="server" /> <br /> <asp:FileUpload ID="fileUpload" runat="server" /> <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" /> <br /> <asp:Label ID="lblMessage" runat="server"></asp:Label> </div> </form> </body> </html> 后台代码: //上传按钮单击事件 protected void btnUpload_Click(object sender, EventArgs e) { if (fileUpload.HasFile) { //获取上传的文件名 string fileName = fileUpload.FileName; //获取上传文件的文件后缀名 string fileFix = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower(); if (fileFix != "png" && fileFix != "jpg" && fileFix != "jpeg" && fileFix != "gif") { this.lblMessage.Text = "上传的文件不是图片类型文件"; } else { fileUpload.SaveAs(Server.MapPath(".") + "//UploadPic//" + fileName); this.imagePic.ImageUrl = "~/UploadPic/" + fileName; this.lblMessage.Text = "图片上传成功!"; } } } 这是一篇过去很久的文章,其中的信息可能已经有所发展或是发生改变。 ...

2016-11-22 · 1 分钟 · 277 字 · CSharp