2015-0107

ASP.NET MVC5 post提交数据时报:An asynchronous module or handler completed while an asynchronous operation was still pending.错误,测试的时候提交正常数据不报错,如果提交包含javascript和html等数据的时候报以上错误,经测试系安全验证异常错误,但是界面上并未显示相关异常信息


解决方法,在Action上添加[ValidateInput(false)]Attribute关闭验证就不会报错,但是安全性考虑建议使用AntiXSS白名 阅读全文>>

标签: asynchronous MVC5 AntiXSS 阅读:9847
2014-1219

用如下方法获取UserId报空引用异常

public class BaseController : Controller
{
    protected SiteContext db = new SiteContext();
    protected Guid userId;
    public BaseController()
    {
        userId = Guid.Parse(User.Identity.GetUserId());
    }
}

由于Controller未初始化完成,User为空,重写初始化方法,在初始化(ba 阅读全文>>

标签: MVC5 Controller User Null 阅读:15464
2014-1202

使用如下代码加载css:

<link href="/Content/css/home?v=zhVOIpUNuvCOZhJyBcQWpMlozayor4te6k-pM29wHqI1" rel="stylesheet"/>


报403 - 禁止访问: 访问被拒绝。
您无权使用所提供的凭据查看此目录或页面。


解决方法:

如上链接MVC Bundle的处理规则是:

如果服务器存在/Content/css目录,则按照常规文件请求文件系 阅读全文>>

标签: ASP.NET MVC BundleStyle BundleScript 403错误 阅读:10946
2013-1126

1 X-AspNetMvc-Version
在Global.asax的Application_Start方法中加入

  MvcHandler.DisableMvcResponseHeader = true;

2 X-AspNet-Version

web.config中
<httpRuntime enableVersionHeader="false" />

3 X-Powered-By

web.config中
<httpProtocol>
       <customHeaders>
    阅读全文>>

标签: Server X-AspNet-Version X-AspNetMvc-Version X-Powered-By 阅读:14299
2013-0805

我们在用mvc开发的时候新增数据提交表单时,ModelState.IsValid 提示The ID field is required.


我们先看下Model设计:


[Key]
[Editable(false,AllowInitialValue=true)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }


解说下,这里我们使用自动增长主键,切不允许编辑属性... 阅读全文>>

标签: MVC ModelState.IsValid 阅读:24391
2013-0730

MVC使用Ajax.BeginForm上传图片时HttpPostedFileBase file为null,Request.Files获取不到文件,问题分析是页面中存在jquery.unobtrusive-ajax.js文件,注释后能够获取到。改为Html.BeginForm实现如下:

@using (Html.BeginForm("UploadPhoto", "EditUserInfo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
选择图片:
<input type="file" name="uploa 阅读全文>>
                
标签: mvc Ajax.BeginForm HttpPostedFileBase 图片上传 文件上传 阅读:37314
2013-0725

在asp.net mvc的强类型视图(strongly typed view)使用下拉列表,在Controller中设定的选择项在View上选中失效


下面是我的代码:

public ActionResult Create(int id=0,int parentid=0)
{
    var productCategory=db.ProductCategorys.Where(pc => pc.ParentID == parentid).ToList();
    productCategory.Insert(0,new ProductCategoryModel() { Name = "顶级分 阅读全文>>
                
标签: ASP.NET MVC SelectList selected 阅读:11079
2013-0724

mvc使用Area分区开发后,存在在不同Area间跳转,需要为每个区间添加Area规则,如下:

public class CompanyAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Company";
        }
    }
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context 阅读全文>>
                
标签: ASP.NET MVC RedirectToAction Area 阅读:20179
2013-0529
[Display(Name="发布时间:")]
[DataType(DataType.Date)]
public DateTime PubTime { get; set; }


修改如下:

[Display(Name="发布时间:")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime PubTime { get; set; }


说明 Html5的 阅读全文>>

标签: MVC4 DataType.Date EditorFor Html5 type="date" input 阅读:18687
2013-0510

在SEO搜索优化中都有要求提交SiteMap的,例如google,百度都有这个功能。具体意义不再本文讨论中。


几个重要的问题

  1. 生成文章类动态sitemap.xml文件

  2. 生成sitemapindex.xml文件

  3. 何时调整以上两个文件做到时效性


第一步生成以上两个xml文件,以下代码放于HomeCo 阅读全文>>

标签: MVC SiteMap SiteMapIndex Xml 阅读:16298
2013-0510

网站要对外提供资源订阅,自然少不了Rss支持,Rss就是遵循Rss格式的xml文档,网站能自动生成这个自然能方便很多。

示例:http://www.yn-s.com/Home/Rss


腾讯也提供邮箱订阅功能,前提是你网站要支持rss。


以下是mvc实现此功能代码:

public ActionResult Rss()
        {
                var articles =db.Articles.OrderByDescending 阅读全文>>
                
标签: MVC RSS 资源订阅 阅读:13887
2013-0502

首先FilterConfig添加

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new CustomHandleErrorAttribute());
}


自定义HandleErrorAttribute如下:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true) 阅读全文>>
                
标签: HandleErrorAttribute MVC4 CustomHandleErrorAttribute 阅读:20262
2013-0413

申请时Token验证代码如下:

private static readonly string Token = "wweixin";
        //
        // GET: /Weixin/
        [HttpGet]
        public ActionResult Index()
        {
            string echoStr = Request.QueryString["echoStr"];
            if (CheckSignature())
            {
                if (!string.IsNullOrEmpty(echoStr))
           阅读全文>>
                
标签: 微信 公众平台 Token mvc 阅读:34528
2013-0322

mvc使用MVC Ajax.BeginForm提交的时候有重复提交结果的时候检查相关js文件引用情况,

其中mvc4注意

@Scripts.Render("~/bundles/modernizr")
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                       "~/Scripts/jquery.unobtrusive*",
                       "~/Scripts/jquery.validate*"));

这个里边他是通过*号加载了符合... 阅读全文>>

标签: MVC Ajax.BeginForm 重复提交 阅读:15843
2013-0307

前端模版文件,给第一个初始下拉列表初始值,第二个没数据,通过Ajax调用后台返回json数据绑定。


@Html.DropDownList("CategroyID", (IEnumerable<SelectListItem>)ViewBag.CategoryID, "请选择...", new { id = "CategroyID", onchange = "GetArticleClass(this)" })
<select id="ArticleClassID" name="ArticleClassID">
<option value="">请选择.. 阅读全文>>
                
标签: MVC Ajax Json 级联下拉列表 阅读:15095
2013-0125

MVC4 SimpleMembership 身份验证,重新删除数据库重新生成后注册用户登录,遇到User.IsUserInRole()或者[Authorize(Roles = "xxx")]方法后报InvalidOperationException:No user found was found that has the name xxx,查询数据数据都正常啊,后边换了个浏览器发现一切正常,把原浏览器身份验证的cookie删除后也一切正常。

SimpleMembership身份验证过多的依赖于cookie,数据变了后为什么不能自动修改或删除无效数据,唉 阅读全文>>

标签: MVC4 SimpleMembership 阅读:11438
2013-0121

mvc4中把之前Membership升级成simpleMembership,自然升级是好处比较多,每一个新版本的到来,都需要接收它的惊喜和接受它的bug。

我在Controller上做验证的时候,我们使用 [Authorize] 和 [Authorize(Users="myuser")]都没问题,当我们使用 [Authorize(Roles="admin")]的时候问题来了,我们得到以下错误:

Server Error in '/' Application.


A network-related or i 阅读全文>>

标签: SimpleMembership MVC4 AuthorizeAttribute Roles 阅读:219007
2013-0120

报错如下:

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The reque 阅读全文>>

标签: MVC Areas Route 阅读:16605
2012-1225
一、XSS攻击
  默认情况下,从@表达式生成的所有文本都是HTML编码过的,但由于某些情况下要显示HTML文本时,必须对于进行白名单过滤。
  使用微软的 HtmlSanitizationLibrary.Dll库进行白名单过滤
  Sanitizer.GetSafeHtmlFragment(InputHtml);

二、SQL注入
  对所有的SQL语句及参数进行全面的过滤

三、防止CSRF(跨网站请求伪造),只针对POST请求
  Action前加入[Vali 阅读全文>>
标签: ASP.NET mvc 安全 阅读:12193
2012-1113

 安装asp.net mvc4之后,之前的mvc3项目编译时报这个错“The type System.Web.Mvc.ModelClientValidationRule exists in both c:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll and c:\Program Files\Microso...”

重新安装mvc3也许可以解决这个问题,不过还会覆盖mvc4的配置,可以通过修改引用来解决:

1.打开根目录下web.config文件添加一下配置项:

<appse 阅读全文>>

标签: MVC3 MVC4 阅读:14363
2012-0131
<%: ViewData["Message"] %>

冒号的绑定可以自动转换HTML编码的代码(无法在前台显示在后台拼接的HTML代码,而使用 Response.Write(pager.MvcPager()) 又是正常显示的)

<%= Model.ModelPagerHtmls %>

等号的绑定没有这种机制
标签: ASP.NET MVC 数据绑定 阅读:14027