【问题标题】:Razor Virtual Path issueRazor 虚拟路径问题
【发布时间】:2011-09-11 18:51:59
【问题描述】:

控制器是:

public class HomeController : Controller
{
    public ActionResult LogOn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogOn(string captcha)
    {
        if (captcha == HttpContext.Session["captcha"].ToString())
            return Content("ok");
        else
            return Content("failed");
    }

    public CaptchaImageResult ShowCaptchaImage()
    {
        return new CaptchaImageResult();
    }
 }

观点是:

<%using (Html.BeginForm())
{ %>
   <p><img src="/Home/ShowCaptchaImage" /></p>
   <p>Please enter the string as shown above:</p>
   <p><%= Html.TextBox("captcha")%></p>
   <p><input type="submit" value="Submit" /></p>
<% } %>

一切顺利,验证码图像被渲染(CaptchaImageResult 在响应流中写入一个jpg)。但如果我使用剃刀视图引擎,则不会呈现验证码图像。

如何解决?

还有一件事:这是显示验证码的好方法吗?

稍后编辑:&lt;img src=@Href("../../Home/ShowCaptchaImage") /&gt; 工作正常。 问题不是因为剃刀而是因为在第一个示例中我使用的是 Visual Studio 开发服务器,而在第二个示例中我使用的是 IIS

【问题讨论】:

  • 使用razor视图引擎时,返回的Html是什么样的?

标签: c# .net asp.net-mvc-3 razor virtual-path


【解决方案1】:
<img src="/Home/ShowCaptchaImage" />

这会导致浏览器从您托管应用程序的 HTTP 源(方案+域+端口)的根目录请求 /Home/ShowCaptchaImage。您需要映射 URL 以反映托管应用程序的任何虚拟文件夹等内容。如果我没看错,这就是您面临的问题,请查看ResolveUrl/ResolveClientUrl equivalents for Asp.Net Razor?(我没有测试过,但看起来不错)。

此外,您可以使用 Firebug 或其他浏览器中的等效工具轻松诊断问题 - 查看发出的请求,您将看到浏览器实际请求的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2013-02-28
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多