Force to 404 in View
# help-with-umbraco
c
Umb 10.6.1 I have a perfectly working 404 page set up. However, I want to force a redirect to it from within a View if the Model's "Approved" toggle isn't "On". So something like this:-
Copy code
if (!Model.Approved) {
        throw new HttpException(404, "Not Found");
    }
Or Ideally a Redirect, as I don't think throwing exceptions is very healthy. This is just a guess but does anyone know the right syntax within an UmbracoViewPage? Thanks.
h
Shouldn't you be using a 403 or 409 for this situation? I would probably deal with this in a render controller rather than the view.
c
I'm using a plain UmbracoViewPage to display a node's content, no need for a special controller (gasp!). If the page is not approved, then I don't want it to exist as far as the outside world is concerned, therefore a 404 is appropriate. The question is how to redirect from a View. Once upon a time (in the golden Classic age) you could just do Response.Redirect to where ever you wanted. Not now of course 😉
h
You should be able to still do that (although not recomended 😄) try
Copy code
csharp
Context.Response.Redirect(...);
return;
The return is important.
c
Spot on, thanks. Sometimes, it's just the little things 😉
6 Views