In my MVC application, the controllers are being created using spring IOC as controller factory. If the user is requesting for a wrong controller by editing the url in the browser I am displaying in the browser 'the resource is not exist' message. Instead I want to direct him to the login page of the application.
public class ControllerFactory : IControllerFactory
{
private static readonly ILog log =
LogManager.GetLogger(typeof(ControllerFactory));
public IController CreateController(RequestContext requestContext, string controllerName)
{
log.Debug("ControllerFactory.CreateController :controllerName =" + controllerName);
controllerName = controllerName.ToLower();
IApplicationContext ctx = ContextRegistry.GetContext();
Controller ControllerObj = null;
if(ctx.ContainsLocalObject(controllerName))
{
ControllerObj = (Controller)ctx[controllerName];
log.Debug("Controller Object is created :" + controllerName);
}
else
{
//Showing error message
requestContext.HttpContext.Response.Write(String.Format("<br/><b><valign=\"center\"><Font Size=\"6\" Font Color=\"Red\"> The Resource {0} is not available</b>", controllerName));
// **Insteadd of showing the above message I want to direct user to the login page.**
// **"Account/Login"**
log.Error("there is no controller defintion with " + controllerName);
requestContext.HttpContext.Response.End();
}
return ControllerObj;
}
public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
{
return SessionStateBehavior.Default;
}
public void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
How can I redirect user to login page("/Account/Login") instead of showing error message?
Did you try requestContext.HttpContext.Response.Redirect(url) ?
I suppose UrlHelper will also have hardcoded controller and action names, e.g.
UrlHelper url = new UrlHelper(Request.RequestContext);
var result = url.Action("Login", "Account");
But with T4MVC(http://t4mvc.codeplex.com/) you can do this:
var result = url.Action(MVC.Account.Login());
Related
In ASP.NET MVC 5, I'm trying to extend my site's general Error Handling and I'm having trouble to cover ActionResult and JsonResult in once. I'm aware that JsonResult is divered from ActionResult, but when I return ActionResult I just give MVC a View, and for JsonResult, I handle the result with Javascript.
So the case, where the return value for the Action is an ActionResult, and the Application_Error() returns a JsonResult, the Json with just be printed in the view. What are you supposed to do in that situation? I rather have it to redirect to errorpage or login screen. I don't even know if I'm supossed to craft so javascript that handle the catch of the json to manually redirect or there is some "built-in" MVC mechanisme I should use.
In a simplified example I have the two Action's:
public ActionResult ShowPackages(PackagesViewModel packagesViewModel)
{
var model = PackageBalhelper.GetPackageViewModel(packagesViewModel);
return View(model);
}
[HttpPost]
public JsonResult GetDatatableRows(PackagesViewModel packagesViewModel)
{
var jsonResult = CreateJsonResult(packagesViewModel);
return jsonResult;
}
What I've so far (From MSDN)
protected void Application_Error()
{
HttpContext httpContext = HttpContext.Current;
if (httpContext != null)
{
var requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext;
// When the request is ajax the system can automatically handle a mistake with a JSON response. Then overwrites the default response
if (requestContext.HttpContext.Request.IsAjaxRequest())
{
httpContext.Response.Clear();
string controllerName = requestContext.RouteData.GetRequiredString("controller");
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(requestContext, controllerName);
ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller);
var jsonResult = new JsonResult
{
Data = new
{
success = false,
serverError = "500"
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
jsonResult.ExecuteResult(controllerContext);
httpContext.Response.End();
}
else
{
LogHelper.WriteMessageToLog("In 'Application_Error'. Not an ajax request'.");
//httpContext.Response.Redirect("~/Error");
}
}
}
What are you supposed to do in that situation? I rather have it to
redirect to errorpage or login screen.
You are on the right track. We definitely do not want to return View (redirect to custom error page), if the client expects Json response from server. Otherwise, it will mess up the client's logic.
This answer might not answer your question directly. However, if you see yourself returning JsonResult a lot, you might want to consider using Web API Controller in ASP.NET MVC 5.
Web API 2.1 supports GlobalExceptionHandler in which you can customize the Http response that is sent when an unhanded application expcetion occurs.
In my case, I use Angular with ASP.NET MVC and Web API. So, I have to return unhandled exception in Json format for Ajax requests.
WebApiExceptionHandler.cs
If your application throws custom exception, you can even filter them here, and return appropriate message.
public class WebApiExceptionHandler : ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
var exception = context.Exception;
var httpException = exception as HttpException;
if (httpException != null)
{
context.Result = new WebApiErrorResult(context.Request,
(HttpStatusCode) httpException.GetHttpCode(), httpException.Message);
return;
}
/*if (exception is MyCustomException)
{
context.Result = new WebApiErrorResult(context.Request,
HttpStatusCode.NotFound, exception.Message);
return;
}*/
context.Result = new WebApiErrorResult(context.Request,
HttpStatusCode.InternalServerError,
"An error occurred while processing your request.");
}
}
WebApiErrorResult.cs
public class WebApiErrorResult : IHttpActionResult
{
private readonly string _errorMessage;
private readonly HttpRequestMessage _requestMessage;
private readonly HttpStatusCode _statusCode;
public WebApiErrorResult(
HttpRequestMessage requestMessage,
HttpStatusCode statusCode,
string errorMessage)
{
_requestMessage = requestMessage;
_statusCode = statusCode;
_errorMessage = errorMessage;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(_requestMessage.CreateErrorResponse(_statusCode, _errorMessage));
}
}
WebApiConfig.cs
Finally, we register our custom exception handler with framework.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
config.Services.Replace(typeof(IExceptionHandler), new WebApiExceptionHandler());
config.Filters.Add(new AuthorizeAttribute());
}
}
Short story about what I am doing and why. I had been doing view to string conversion in Mvc project, but suddenly all project moved to the REST API. But I had to use razor engine to convert my view with all model data there, so I was trying to use directly from api, but it didn't work for me, so I decided to create a Mvc controller and call it directly from API, what is missing, only ControllerContext, because when I create controller directly, it appears to be null.
Here is my Api controller
public class AttachmentController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> Get(long id)
{
try
{
var mvcController = new AttachmentMvcController();
var result = await mvcController.Get();
return Ok(result);
}
catch (Exception e)
{
return InternalServerError(e);
}
}
}
and this is my Mvc Controller
public class AttachmentMvcController : Controller
{
public AttachmentMvcController(){ }
public async Task<byte[]> Get()
{
string result;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// HERE IS MY PROBLEM, I NEED this.ControllerContext, but it is null !!!
if (ControllerContext == null)
{
// create it and do task
var factory = DependencyResolver.Current.GetService<IControllerFactory>() ?? new DefaultControllerFactory();
AttachmentMvcController controller = factory.CreateController(ctx.RequestContext, "AttachmentMvc") as AttachmentMvcController;
RouteData route = new RouteData();
route.Values.Add("action", "ActionThatUsesControllerContext"); // ActionName, but it not required
ControllerContext newContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), route, controller);
controller.ControllerContext = newContext;
result = await controller.ActionThatUsesControllerContext(id);
}
else
{
result = await this.ActionThatUsesControllerContext(id);
}
return result;
}
private async Task<byte[]> ActionThatUsesControllerContext()
{
{....}
// here I am trying to use helper that uses that controller context
string viewAsString = ControllerContext.RenderPartialToString("MyView", requestModel);
{....}
}
}
If anyone has idea how to get that ControllerContext or any other ways to get my razor engine render view inside ApiController, please share.
We have built an MVC app that publishes a complete website with hierarchal Folders, SubFolders and Pages. The resulting pages, are strictly HTML and are not published in our MVC app. Our customers are able to name their Folders and Pages with any compliant string they choose. So conceivably, once the site is hosted, they could end up with a URL such as:
someDomain.com/folder/subfolder1/subfolder2/page-slug. There is no limit to the number of nested subfolders.
We would like to replicate their sites in our MVC app, so that they are able to test them before they publish and perhaps so we can provide hosting ourselves if required.
The obvious problem, is how can we handle,
ourMVCApp.com/folder/subfolder1/subfolder2/page-slug in an MVC app?
If there was a way that we could set routing to handle such a thing, then we could easily get the content required for the request by splitting the url into an array by "/".
The last segment would be a page contained in the previous segment's folder. We could then search our DB using these strings to get the required content.
Your help is greatly appreciated.
FURTHER QUESTION:
In response to the answer provided by Tomi.
I added the code to my controller's class but I am receiving the following warning:
I am not sure what I am missing? Did I put the code in the place? Thanks again.
UPDATE 2. I realized I had not actually created the controller factory, so I followed a partial example I found here: http://develoq.net/2010/custom-controller-factory-in-asp-net-mvc/. And since implementing it, I no longer receive any build-errors, but when I run the the debug, it crashes the built-in IISEXPRESS without any error message.
Here is my controller factory code:
public class FolderControllerFactory : IControllerFactory
{
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
try
{
// Get the path
string path = requestContext.RouteData.Values["pathInfo"].ToString();
IController controller = new FolderController(path);
return controller;
}
catch
{
// Log routing error here and move on
return CreateController(requestContext, controllerName);
}
}
public void ReleaseController(IController controller)
{
var disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
{
return SessionStateBehavior.Default;
}
}
Here is my global:
ControllerBuilder.Current.SetControllerFactory(typeof(ProofPixApp.Controllers.FolderControllerFactory));
And finally my controller:
public class FolderController : Controller
{
private string _path;
public FolderController(string path)
{
_path = path;
}
public ActionResult Index(string name)
{
ViewBag.Message = "Hello " + name;
return View("/Views/" + _path);
}
}
A couple of notes:
1. I removed the 'override' from public IController CreateController
because I kept receiving the initial error I posted.
2. I added public void ReleaseController and the public
SessionStateBehavior GetControllerSessionBehavior methods to the
CreateController class to avoid other build errors.
3. I removed 'base.' from the catch clause because it too was causing a
build error.
SOLUTION:
I was able to avoid the error by checking to see pathValue was not null in the createController method, like so:
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
// Get the path
string path = "";
if (requestContext.RouteData.Values["pathInfo"] != null)
{
path = requestContext.RouteData.Values["pathInfo"].ToString();
}
IController controller = new FolderController(path);
return controller;
}
I have no idea what page slug is but here's my solution on how to achieve the routing you requested.
I made a custom ControllerFactory which handles the url and passes it to controller. This ControllerFactory constructs the controller we use to handle folder-route requests. We get the path from routevalues and then pass it to the FolderController.
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
try
{
// Get the path
string path = requestContext.RouteData.Values["pathInfo"].ToString();
IController controller = new FolderController(path);
return controller;
}
catch
{
// Log routing error here and move on
return base.CreateController(requestContext, controllerName);
}
}
Here's the controller. The actionmethod, which redirects to given path is called Index for now. The actionmethod returns view it finds from the url.
public class FolderController : Controller
{
private string _path;
public FolderController(string path)
{
_path = path;
}
public FolderController()
{
}
public ActionResult Index(string name)
{
ViewBag.Message = "Hello " + name;
return View("/Views/"+_path);
}
}
Last step is to write our own route and register the factory. Open up RouteConfig.cs. My new RegisterRoutes method looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Dynamic",
url: "{*pathInfo}",
defaults: new { controller = "Folder", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
In global.asax we need to register our FolderControllerFactory by adding this line into Application_Start method
ControllerBuilder.Current.SetControllerFactory(typeof(FolderControllerFactory));
And that's it! There's still much to be done, like handling improper urls and such. Also I don't think this supports plain html files, the files must be in .cshtml or asp format.
Here's the test:
My folder structure:
Url I request:
localhost:port/Mainfolder/Subfolder/Subfolder2/view.cshtml?name=Tomi
The result with Route Debugger plugin:
My goal is to find a controller from it's name and area. I have successfully done this if my current httpContext is within the same Area as the to-be-found controller. However, I cannot get my call to the ControllerFactory to take Area into consideration. Here's my code:
public static ControllerBase GetControllerByName(this HtmlHelper htmlHelper, string controllerName)
{
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(htmlHelper.ViewContext.RequestContext, controllerName);
if (controller == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The IControllerFactory '{0}' did not return a controller for the name '{1}'.", factory.GetType(), controllerName));
}
return (ControllerBase)controller;
}
Since it's taking a RequestContext as a parameter, I've added an route value of "area" to it but with no change. Is there something I can do with the requestContext to some how take area into consideration? Do I need to override the controller factory--and if so, what in particular handles Area distinction?
Update:
Here is an example of a AreaRegistration I have:
public class StoresAreaRegistration : AreaRegistration
{
public override string AreaName { get { return "Stores"; } }
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
AreaName,
AreaName + "/{controller}/{action}/{id}",
new { area = AreaName, controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
The Area and Namespaces used to locate a controller are in the RouteData of the RequestContext. They are populatd by default based off of the request you are currently serving up, if you need to change them you have to do so before calling CreateController. You may get an exception when a controller cannot be found so you'll have to account for that as well.
UPDATE: Note, you MUST create a new RequestContext. If you reuse the existing one it will mess with the resolution of actions & views later on down the line in this request.
var tempRequestContext = new RequestContext(Request.RequestContext.HttpContext, new RouteData());
tempRequestContext.RouteData.DataTokens["Area"] = "";
tempRequestContext.RouteData.DataTokens["Namespaces"] = "YourCompany.Controllers";
var controller = ControllerBuilder.Current.GetControllerFactory()
.CreateController(tempRequestContext, "ControllerName");
if(controller != null)
{
//TODO: Implement your logic here
}
protected internal RedirectToRouteResult RedirectToAction(
string actionName,
string controllerName);
means I cannot do
public static ActionResult RedirectToErrorAction(this Controller controller)
{
// redirect to custom error page
return controller.RedirectToAction("error", "support");
}
Any ideas?
You might want to consider returning a custom, shared error view instead, but if you really need to do this, you might want to consider using reflection to invoke the internal method. The former could be implemented in a base controller that becomes the foundation for all your controllers.
First example:
public class BaseController : Controller
{
private ActionResult CreateErrorResult( string message )
{
ViewData["message"] = message;
...
return new View( "CustomError" );
}
}
Second example (if getting the internal attribute via reflection works):
public static ActionResult RedirectToErrorAction(this Controller controller)
{
MethodInfo info = typeof(Controller).GetMethod( "RedirectToAction",
BindingFlags.NonPublic|BindingFlags.Instance,
null,
new Type[] { typeof(string), typeof(string) },
null );
return info.Invoke( controller, new object[] { "error", "support" } )
as ActionResult;
}