How can I return a 404 response from an ASP.NET MVC application?

I have a controller that retrieves images from a database, resizes, caches the result on disk and splashes the image as a result Content().

I recently added support for the "Scrape-buster" code on my site. That is, I take a hash of a unique code attached to each image plus some salt, and pass the first few characters of this hash to the user for confirmation during the search. This allows me to prevent people from scraping every image from the site. (Without logging in and scraping HTML, namely.)

In any case, if the code is ScrapeBusterincorrect, I would like to return a 404 error from my controller. Is there a built-in way to do this, or am I looking at creating a custom one ActionResult?

+3
source share
5 answers

The easiest (maybe not the cleanest) way is to throw away HttpException:

public static void Throw404()
{
    throw new HttpException(404, "The resource cannot be found");
}

You must ensure that no one in the call stack catches the exception. Thus, the output will look like a regular ASP.NET Yellow Screen of Death 404.

A cleaner way is to return a custom result:

public sealed class HttpNotFoundResult : ActionResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        context.HttpContext.Response.StatusCode = 404;
    }
}
+6
source

Why not just use the built-in HttpNotFoundResult?

return HttpNotFound ();

+5
source

403, HttpUnauthorizedResult. , , , URL- , "". , .

+2
public class NotFoundViewResult : ViewResult
{
    public ViewResultNotFound()
    {
        ViewName = "NotFound";
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.TrySkipIisCustomErrors = true;
        context.HttpContext.Response.StatusCode = 404;

        base.ExecuteResult(context);
    }
}

NotFound View ~/Views/Shared

+1

, :

public static class HttpErrorResultHelper
{
    public static HttpErrorResult HttpError(this Controller controller, int statusCode)
    {
        return new HttpErrorResult
        {
            StatusCode = statusCode
        };
    }

    public static HttpErrorResult HttpError(this Controller controller, int statusCode, ActionResult chainedAction)
    {
        return new HttpErrorResult
        {
            StatusCode = statusCode,
            ChainedAction = chainedAction,
        };
    }
}

public sealed class HttpErrorResult : ActionResult
{
    public int StatusCode { get; set; }

    public ActionResult ChainedAction { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        context.HttpContext.Response.TrySkipIisCustomErrors = true;
        context.HttpContext.Response.StatusCode = this.StatusCode;

        if (this.ChainedAction != null)
        {
            ChainedAction.ExecuteResult(context);
        }
    }
}

, , , .

this.HttpError(404, this.View("NotFound"));
0

Source: https://habr.com/ru/post/1726588/


All Articles