+1-888-365-2779
Try Now
More in this section

Forums / Project Feather / Custom Error Pages in Sitefinity 8.1, return correct http status code

Custom Error Pages in Sitefinity 8.1, return correct http status code

10 posts, 0 answered
  1. John
    John avatar
    61 posts
    Registered:
    08 Jun 2012
    28 Sep 2015
    Link to this post

    Hello, I am following the instructions here to set up custom error pages using Sitefinity pages. I realize the linked article is geared toward a Webforms solution, but I'd like to achieve the same results with a pure MVC approach, if possible.

    Is it possible to create a custom MVC widget, that can be added to the SF error page, that will return a custom http status code? I have tried a very simple controller that looks like this:

    public class ErrorStatusController : SpotOnControllerBase
    {
        [DisplayName("Status Code to return. (integer: e.g., 404)")]
        public int StatusCode { get; set; }
        [DisplayName("Status Description to return. (e.g., Page Not Found)")]
        public string StatusDescription { get; set; }
     
        public ActionResult Index()
        {
            if (SystemManager.IsDesignMode && !SystemManager.IsPreviewMode)
            {
                return Content(String.Format("This will return a status code of '{0}' and a status description of '{1}'. Edit this control and set the Status Code and Status Description properties to change.", StatusCode, StatusDescription));
            }
             
            return new HttpStatusCodeResult(StatusCode, StatusDescription);
        }
     
    }

    But I'm still always getting a Status of '200'. 

    Is there some way to have an MVC widget force the return status code?

    Thanks in advance for any help!

  2. Joe
    Joe avatar
    2 posts
    Registered:
    30 Mar 2015
    15 Jan in reply to John
    Link to this post
    I tried almost the exact same solution with no luck - did you manage to come up with a solution?
  3. John
    John avatar
    61 posts
    Registered:
    08 Jun 2012
    15 Jan in reply to Joe
    Link to this post

    I was not able to get a widget to set/override the response status code. I'm guessing it's something related to Sitefinity's  MVC implementation. I ended up creating a new layout for 404 errors, and adding code to it like this:

    @{
        if (!SystemManager.IsDesignMode)
        {
            Response.StatusCode = 404;
            Response.StatusDescription = "Page Not Found";
        }
    }

    This works for me, but is definitely not ideal, as this requires a separate layout for each error/status code :(. I wanted to have a widget that I could re-use and just specify the code and message as properties on the widget. No luck so far, though I haven't revisited this since upgrading from v8.1.

     

  4. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    26 Jan
    Link to this post
    Hello,

    Please, refer to the following KB article in order to achieve the desired result:
    http://www.sitefinity.com/developer-network/knowledge-base/details/custom-error-pages-with-sitefinity-mvc-and-feather

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  5. Will
    Will avatar
    10 posts
    Registered:
    16 Mar 2011
    11 Mar in reply to Nikola Zagorchev
    Link to this post

    Hi, I'm newer to mvc....

    How do I actually apply the info from the KB article?  Am I still making a widget to drop on a page? Is most of the code/logic going in a controller? What will I put in the model and view?

     

    Thanks!

  6. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    12 Mar
    Link to this post
    Hi,

    In the controller, you just set the response status to be 404 and return the view you want to show for the 404 page.
    public ActionResult Index()
    {
        if (!SitefinityContext.IsBackend)
        {
            Response.Status = "404 Not Found";
            Response.StatusCode = 404;
        }
      
        return View("Default");
    }

    The FeatherActionInvokerCustom is just a separate class for the Action Invoker. The code in the Global application class is for registering the Action Invoker.

    I hope that information helps.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  7. Will
    Will avatar
    10 posts
    Registered:
    16 Mar 2011
    04 May in reply to Nikola Zagorchev
    Link to this post
    Like those above, I still haven't been able to get this to work... Nikola, is the actionresult the only code i need at all or is there something else i need to do?
  8. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    04 May
    Link to this post
    Hello Will,

    You need the FeatherActionInvokerCustom as well as the MVC widget to set the response status.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  9. Will
    Will avatar
    10 posts
    Registered:
    16 Mar 2011
    04 May in reply to Nikola Zagorchev
    Link to this post
    Ok, thanks for the quick reply, and sorry for my ingorance, but where do I put each of the pieces of code from the kb article?  Is every single clip of code going in my same controller? If not, can you explicitly help me know where they should exist?
  10. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    10 May
    Link to this post
    Hi,

    If you use the Resolution (you are using Feather 1.3.350.0 or newer), you need to have the following files:
    FeatherActionInvokerCustom.cs - the FeatherActionInvokerCustom class;
    Global.asax - the Global application class (Application_Start and Bootstrapper_Bootstrapped methods);
    MVC widget that will just set the response status code. You will place the widget in the error page.

    Hope this helps.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
10 posts, 0 answered