Redirect page without view to 404

Vote:
 


I have a blog listing page under which is blog category page under which is blog individual page as hierarchy in admin/pages of back office. i don't have view for second category page. When I enter url for second page I am suppose to get 404 page, however I am getting 500 error. How to handle this?

#205958
Jul 30, 2019 9:46
Vote:
 

What is the stacktrace of the 500 error you are getting? 

#205959
Jul 30, 2019 10:04
Vote:
 

System.InvalidOperationException: The view '~/Views/Blog.../Index.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Blog.../Index.cshtml
at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_6.<BeginInvokeAction>b__4()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

#205960
Jul 30, 2019 10:36
Vote:
 

Hi Muller,

If you are accessing a page that doesn't have a view then it will give you 500, not 404 because 404 status code is for page not found(in your case you have a page). So make sure you should have view associated with your page type or block type.

According to your stack trace, it is searching the view at "~/Views/Blog.../Index.cshtml" location so make sure you create a view inside this folder.

Thanks

Ravindra

#205963
Jul 30, 2019 12:12
Vote:
 

Hi,

Does your second level page type have a controller associated with it? If your page type has a controller but no view you'll get an error like the one above. If it has no controller you should get a 404.

If your page type does have a controller then the easiest route would be to remove the controller but if, for some reason, you can't get rid of that controller (e.g. it's set up to handle a base type) you could take the approach used in the Alloy site and, in your IViewTemplateModelRegistrator, include something like the following:

public static void OnTemplateResolved(object sender, TemplateResolverEventArgs args)
{
    //Disable DefaultPageController for page types that shouldn't have any renderer as pages
    if (args.ItemToRender is IContainerPage && args.SelectedTemplate != null && args.SelectedTemplate.TemplateType == typeof(DefaultPageController))
    {
        args.SelectedTemplate = null;
    }
}

where your secondary blog page would implement IContainerPage and IContainerPage is just an empty interface.

#205968
Edited, Jul 30, 2019 12:53
Muller - Aug 01, 2019 4:15
I have implemented IContainerPage to my page. But I have separate controller for the page and args.SelectedTemplate.TemplateType is my blogcontroller which makes the condition false.
Paul Gruffydd - Aug 01, 2019 10:19
If the page has a separate controller but no view, could you not just get rid of the page controller?

The code snippet I posted was just a sample from the Alloy site which prevents pages implementing IContainerPage from being rendered by the default page controller which acts as a fallback for pages which don't have a controller of their own. If an IContainerPage has a specific controller defined, it will still use that. In your situation, if you can't get rid of the controller, it sounds like you need to either change "args.SelectedTemplate.TemplateType == typeof(DefaultPageController)" to "args.SelectedTemplate.TemplateType == typeof(BlogController)" or just remove that type check altogether so that instances of IContainerPage never resolve to a controller.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.