Custom Error Pages in Sitefinity 8.1, return correct http st

Posted by Community Admin on 05-Aug-2018 20:17

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

All Replies

Posted by Community Admin on 28-Sep-2015 00:00

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!

Posted by Community Admin on 15-Jan-2016 00:00

I tried almost the exact same solution with no luck - did you manage to come up with a solution?

Posted by Community Admin on 15-Jan-2016 00:00

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.

 

Posted by Community Admin on 26-Jan-2016 00:00

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
 

Posted by Community Admin on 11-Mar-2016 00:00

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!

Posted by Community Admin on 12-Mar-2016 00:00

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
 

Posted by Community Admin on 04-May-2016 00:00

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?

Posted by Community Admin on 04-May-2016 00:00

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
 

Posted by Community Admin on 04-May-2016 00:00

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?

Posted by Community Admin on 10-May-2016 00:00

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
 

Posted by Community Admin on 13-Mar-2017 00:00

Hey,

 Still can't get it to work. Here is what I did

  1- Create new controller ErrorStatusController, Register it [ControllerToolboxItem(Name = "ErrorStatus", Title = "Error Status", SectionName = "CustomCMS")]

public ActionResult Index()                      Response.Status = "404 Not Found";             Response.StatusCode = 404;             Response.StatusDescription = "Not Found!";             return View(nameof(Index));         

I had issue registering it, however end up doing it on the backend

2- Adding View Simply just showing <h2>Error Page 404</h2>

3- Add FeatherActionInvokerCustom

4- Update Global.asac adding FeatherActionInvokerCustom.Register();  inside Bootstrapper_Bootstrapped

5- on backend I had (default,404,500), I used it when I were doing on application level that caused some issue logging me out after certain time when I work on backend.

   On default, I dropped Error Status mvc and still showing The controller with the name 'projectName.Sitefinity.Mvc.customFolderName.Controllers.ErrorStatusController' cannot be resolved.

However, 2 things to mention

1- I didn't update/add anything to web.config

2- I had issue when I create controller saying folder/files exist show all. I fixed that but could be an issue.

 

Any thought what I'm missing ?

Can you provide sample for handling 404,500 ?

 

Thank You

Mansi

 

 

 

This thread is closed