What is the proper way to send an HTTP 404 response from an ASP.NET MVC action?

前端 未结 6 696
春和景丽
春和景丽 2020-12-02 06:25

If given the route:

{FeedName}/{ItemPermalink}

ex: /Blog/Hello-World

If the item doesn\'t exist, I want to return a 404. What is the

6条回答
  •  误落风尘
    2020-12-02 06:56

    We do it like so; this code is found in BaseController

    /// 
    /// returns our standard page not found view
    /// 
    protected ViewResult PageNotFound()
    {
        Response.StatusCode = 404;
        return View("PageNotFound");
    }
    

    called like so

    public ActionResult ShowUserDetails(int? id)
    {        
        // make sure we have a valid ID
        if (!id.HasValue) return PageNotFound();
    

提交回复
热议问题