Display View in folder without Controller or Action in ASP.net MVC

后端 未结 4 1507
春和景丽
春和景丽 2021-01-15 17:29

I would like to display the view in the folder if no controller/action matches.

For example www.site.com/Home/Index, if I have the normal default route {controller}/

4条回答
  •  不思量自难忘°
    2021-01-15 18:07

    For missing actions you can override HandleUnknownAction.

    For missing controllers you can implement a custom DefaultControllerFactory and override GetControllerInstance with something like this:

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
    
       if (controllerType == null)
          return new DumbController();
    
       return base.GetControllerInstance(requestContext, controllerType);
    }
    
    class DumbController : Controller {
    
       protected override void HandleUnknownAction(string actionName) {
    
          try {
             View(actionName).ExecuteResult(this.ControllerContext);
          } catch (Exception ex) {
             throw new HttpException(404, "Not Found", ex);
          }
       }
    }
    

提交回复
热议问题