 悬赏园豆:5
                [已解决问题] 
            
                    解决于 2015-01-14 16:49
                悬赏园豆:5
                [已解决问题] 
            
                    解决于 2015-01-14 16:49 
                 
        我再网上按照http://www.cnblogs.com/bingguang/p/3610537.html这个里面的代码做的产生验证码。可是为什么是这样的?不显示,src里面不是这样么?总感觉src里面的代码没起作用
看了你发的那篇链接,它是返回 FileContentResult,
我是从来没有返回这个result类型,我是这么做的,在验证码生成类中,直接输出图片,同时在Action中返回null,如下:
public class VerifyController : Controller
    {
        [HttpGet]
        public ActionResult GetVerifyCode()
        {
            Session["VerifyCode"] = null;
            string verifyCode = VerifyCode.Instance.CreateValideCode(this.Response);
            Session["VerifyCode"] = verifyCode.ToUpper();
            return null;
        }
    }
在CreateValideCode()中,直接输出图片,如下:
string chkCode=string.Empty;
try
           {
               bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
               response.ClearContent();
               response.ContentType = "image/Png";
               response.BinaryWrite(ms.ToArray());
           }
           finally
           {
               //显式释放资源 
               bmp.Dispose();
               g.Dispose();
           }
           return chkCode;
在cshtml中,调用为:
<img alt="点击刷新验证码" title="点击刷新验证码" id="verifyImg" src="/Verify/GetVerifyCode" style="cursor:pointer;" onclick="RefreshValidateCode(this);" />
能将VerifyCode中的完整代码发给我试试么
@瑶瑶EXO: 一样的呀,我用你发的那个链接里的代码,生成也是可以的,如下:
        [HttpGet]
        public ActionResult GetVerifyCode()
        {
            Session["VerifyCode"] = null;
            string verifyCode = string.Empty;
            byte[] bytes = VerifyCode.Instance.DrawVerifyImage(ref verifyCode);
            if (bytes != null && bytes.Length>0)
            {
                base.Response.ClearContent();
                base.Response.ContentType = "image/jpeg";
                base.Response.BinaryWrite(bytes.ToArray());
                Session["VerifyCode"] = verifyCode.ToUpper();
            }
            return null;
        }

只是,这个验证码超丑。
@king2003: 亲,我里面没有DrawVerifyImage这个方法呀
@瑶瑶EXO: 为了这5个园豆,真不容易啊。你方法名称改下,把DrawVerifyImage,改成BuildImg,同时把生成的验证码通过 ref引用 取出来,不就得了吗。
@king2003: 哈哈,谢谢你啦。 我也找到我先前的原因了,是因为控制器账户权限问题。 非常感谢你。嘻嘻
调试一下看看!最后的这个变量checkCode的值。
checkCode产生了了值。我的Controller为AccountController。一加载的页面是Register。只有我在Register方法中调用getCode()时调试才会进getCode(),不然就不会进,所以我在想,是不是视图img中的SRC无效呢
public ActionResult Register()
 {
 
 return View();
 }
public FileContentResult getCode()
 {
 VerifyCode v = new VerifyCode();
 byte[] bytes = v.BuildImg();
 Session["vcode"] = v.checkCode;
 return File(bytes, @"image/jpeg");
 }
你把SRC的url直接放到浏览器URL里看看
很大可能是验证码的路径错了,贴出代码看一下
我知道了,是控制器账户权限问题。谢谢啦,哈哈