Question

The controller is:

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();
    }
 }

The view is:

<%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>
<% } %>

Everything goes well, the captcha image is rendered (CaptchaImageResult writes a jpg in the response stream). But if i use the razor view engine there is no captcha image rendered.

How to fix it?

And one more thing: is it a good way to display captcha?

Later edit: <img src=@Href("../../Home/ShowCaptchaImage") /> works fine. The problem wasn't because razor it was because in the first example i was using Visual Studio Development server and in the second one i was using IIS

Was it helpful?

Solution

<img src="/Home/ShowCaptchaImage" />

This causes the browser to request /Home/ShowCaptchaImage from the root of the HTTP origin (scheme+domain+port) on which you host the application. You need to map the URL to reflect things like any virtual folders in which the app is hosted. If I'm not wrong and this is the problem you are facing, take a look at ResolveUrl/ResolveClientUrl equivalents for Asp.Net Razor? (I haven't tested it, but it looks good).

Also, you could diagnose the problem easily using Firebug or an equivalent tool in another browser - take a look at the requests which are made and you will see what the browser actually requests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top