Custom Error Pages in Sitefinity 8.1, return correct http status code
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!
I tried almost the exact same solution with no luck - did you manage to come up with a solution?
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.
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
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!
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"
);
FeatherActionInvokerCustom
is just a separate class for the Action Invoker. The code in the Global application class is for registering the Action Invoker.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?
Hello
You need the FeatherActionInvokerCustom as well as the MVC widget to set the response status.
Regards,
Nikola Zagorchev
Telerik
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?
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
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