How to return a view for HttpNotFound() in ASP.Net MVC 3? - asp.net

Is there a way to return the same view every time a HttpNotFoundResult is returned from a controller? How do you specify this view? I'm guessing configuring a 404 page in the web.config might work, but I wanted to know if there was a better way to handle this result.
Edit / Follow up:
I ended up using the solution found in the second answer to this question with some slight tweaks for ASP.Net MVC 3 to handle my 404s: How can I properly handle 404s in ASP.Net MVC?

HttpNotFoundResult doesn't render a view. It simply sets the status code to 404 and returns an empty result which is useful for things like AJAX but if you want a custom 404 error page you could throw new HttpException(404, "Not found") which will automatically render the configured view in web.config:
<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="/Http404.html" />
</customErrors>

This solution combines IResultFilter and IExceptionFilter to catch either thrown HttpException or returned HttpStatusCodeResult from within an action.
public class CustomViewForHttpStatusResultFilter: IResultFilter, IExceptionFilter
{
string viewName;
int statusCode;
public CustomViewForHttpStatusResultFilter(HttpStatusCodeResult prototype, string viewName)
: this(prototype.StatusCode, viewName) {
}
public CustomViewForHttpStatusResultFilter(int statusCode, string viewName) {
this.viewName = viewName;
this.statusCode = statusCode;
}
public void OnResultExecuted(ResultExecutedContext filterContext) {
HttpStatusCodeResult httpStatusCodeResult = filterContext.Result as HttpStatusCodeResult;
if (httpStatusCodeResult != null && httpStatusCodeResult.StatusCode == statusCode) {
ExecuteCustomViewResult(filterContext.Controller.ControllerContext);
}
}
public void OnResultExecuting(ResultExecutingContext filterContext) {
}
public void OnException(ExceptionContext filterContext) {
HttpException httpException = filterContext.Exception as HttpException;
if (httpException != null && httpException.GetHttpCode() == statusCode) {
ExecuteCustomViewResult(filterContext.Controller.ControllerContext);
// This causes ELMAH not to log exceptions, so commented out
//filterContext.ExceptionHandled = true;
}
}
void ExecuteCustomViewResult(ControllerContext controllerContext) {
ViewResult viewResult = new ViewResult();
viewResult.ViewName = viewName;
viewResult.ViewData = controllerContext.Controller.ViewData;
viewResult.TempData = controllerContext.Controller.TempData;
viewResult.ExecuteResult(controllerContext);
controllerContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
You can register this filter so, specifying either the http status code of the HttpException or the concrete HttpStatusCodeResult for which you want to display the custom view.
GlobalFilters.Filters.Add(new CustomViewForHttpStatusResultFilter(new HttpNotFoundResult(), "Error404"));
// alternate syntax
GlobalFilters.Filters.Add(new CustomViewForHttpStatusResultFilter(404, "Error404"));
It handles exceptions and HttpStatusCodeResult thrown or returned within an action. It won't handle errors that occur before MVC selects a suitable action and controller like this common problems:
Unknown routes
Unknown controllers
Unknown actions
For handling these types of NotFound errors, combine this solution with other solutions to be found in stackoverflow.

Useful info from #Darin Dimitrov that HttpNotFoundResult is actually returning empty result.
After some study. The workaround for MVC 3 here is to derive all HttpNotFoundResult, HttpUnauthorizedResult, HttpStatusCodeResult classes and implement new (overriding it) HttpNotFound() method in BaseController.
It is best practise to use base Controller so you have 'control' over all derived Controllers.
I create new HttpStatusCodeResult class, not to derive from ActionResult but from ViewResult to render the view or any View you want by specifying the ViewName property. I follow the original HttpStatusCodeResult to set the HttpContext.Response.StatusCode and HttpContext.Response.StatusDescription but then base.ExecuteResult(context) will render the suitable view because again I derive from ViewResult. Simple enough is it? Hope this will be implemented in the MVC core.
See my BaseController bellow:
using System.Web;
using System.Web.Mvc;
namespace YourNamespace.Controllers
{
public class BaseController : Controller
{
public BaseController()
{
ViewBag.MetaDescription = Settings.metaDescription;
ViewBag.MetaKeywords = Settings.metaKeywords;
}
protected new HttpNotFoundResult HttpNotFound(string statusDescription = null)
{
return new HttpNotFoundResult(statusDescription);
}
protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null)
{
return new HttpUnauthorizedResult(statusDescription);
}
protected class HttpNotFoundResult : HttpStatusCodeResult
{
public HttpNotFoundResult() : this(null) { }
public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { }
}
protected class HttpUnauthorizedResult : HttpStatusCodeResult
{
public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { }
}
protected class HttpStatusCodeResult : ViewResult
{
public int StatusCode { get; private set; }
public string StatusDescription { get; private set; }
public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { }
public HttpStatusCodeResult(int statusCode, string statusDescription)
{
this.StatusCode = statusCode;
this.StatusDescription = statusDescription;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
context.HttpContext.Response.StatusCode = this.StatusCode;
if (this.StatusDescription != null)
{
context.HttpContext.Response.StatusDescription = this.StatusDescription;
}
// 1. Uncomment this to use the existing Error.ascx / Error.cshtml to view as an error or
// 2. Uncomment this and change to any custom view and set the name here or simply
// 3. (Recommended) Let it commented and the ViewName will be the current controller view action and on your view (or layout view even better) show the #ViewBag.Message to produce an inline message that tell the Not Found or Unauthorized
//this.ViewName = "Error";
this.ViewBag.Message = context.HttpContext.Response.StatusDescription;
base.ExecuteResult(context);
}
}
}
}
To use in your action like this:
public ActionResult Index()
{
// Some processing
if (...)
return HttpNotFound();
// Other processing
}
And in _Layout.cshtml (like master page)
<div class="content">
#if (ViewBag.Message != null)
{
<div class="inlineMsg"><p>#ViewBag.Message</p></div>
}
#RenderBody()
</div>
Additionally you can use a custom view like Error.shtml or create new NotFound.cshtml like I commented in the code and you may define a view model for the status description and other explanations.

protected override void HandleUnknownAction(string actionName)
{
ViewBag.actionName = actionName;
View("Unknown").ExecuteResult(this.ControllerContext);
}

Here is true answer which allows fully customize of error page in single place.
No need to modify web.confiog or create sophisticated classes and code.
Works also in MVC 5.
Add this code to controller:
if (bad) {
Response.Clear();
Response.TrySkipIisCustomErrors = true;
Response.Write(product + I(" Toodet pole"));
Response.StatusCode = (int)HttpStatusCode.NotFound;
//Response.ContentType = "text/html; charset=utf-8";
Response.End();
return null;
}
Based on http://www.eidias.com/blog/2014/7/2/mvc-custom-error-pages

Please follow this if you want httpnotfound Error in your controller
public ActionResult Contact()
{
return HttpNotFound();
}

Related

Disable outputcache programmically before it happens that isn't an exception

Is there a way to disable the outputcache here programmatically if something happens that is not an exception?
[OutputCache(CacheProfile = "StatisticSheets")]
public virtual ActionResult GameStatistics(int? eventId, int? divisionId, string ids)
{
If(true) {
// Don't Cache This Page
}
return View();
}
This is how i have done:
create a derive class from outputcache:
public class MyOutputCache : OutputCacheAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
// checking route data for special condition
if(!filterContext.RouteData.Values.TryGetValue("abortcaching",out object _))
{
base.OnResultExecuting(filterContext);
}
}
}
then in controller:
[MyOutputCache(Duration = 60, VaryByParam = "none")]
public ActionResult Index()
{
ViewBag.Title = "Home Page " + DateTime.Now.Minute.ToString();
// it can be your condition
if (DateTime.Now.Minute %2 == 0)
{
RouteData.Values.Add("abortcaching", "true");
}
return View();
}
hope it helps.
If you need global to disable then read the below details, at the end also I shared with action method implementation code.
First, we want to capture whether or not the app is in debugging mode when it is launched. We'll store that in a global variable to keep things speedy.
public static class GlobalVariables
{
public static bool IsDebuggingEnabled = false;
}
Then in the Global.asax code's Application_Start method, write to the global property.
protected void Application_Start()
{
SetGlobalVariables();
}
private void SetGlobalVariables()
{
CompilationSection configSection = (CompilationSection)ConfigurationManager
.GetSection("system.web/compilation");
if (configSection?.Debug == true)
{
GlobalVariables.IsDebuggingEnabled = true;
}
}
Now we will create our own class to use for caching, which will inherit from OutputCacheAttribute.
public class DynamicOutputCacheAttribute : OutputCacheAttribute
{
public DynamicOutputCacheAttribute()
{
if (GlobalVariables.IsDebuggingEnabled)
{
this.VaryByParam = "*";
this.Duration = 0;
this.NoStore = true;
}
}
}
Now when you decorate your controller endpoints for caching, simply use your new attribute instead of [OutputCache].
// you can use CacheProfiles or manually pass in the arguments, it doesn't matter.
// either way, no caching will take place if the app was launched with debugging
[DynamicOutputCache(CacheProfile = "Month")]
public ViewResult contact()
{
return View();
}
With Action Method
-> For .net Framework: [OutputCache(NoStore = true, Duration = 0)]
-> For .net Core: [ResponseCache(NoStore = true, Duration = 0)]
You can use the particular action method above way

MVC basecontroller doesn't return to controller after Initializing ASP.NET

First off all i'm fairly new to C# and ASP.NET (mainly program java).
I've got a BaseController where i want to fill a viewbag for all my other controllers to use. I also set some session data and create some cookies. This is the basecontroller:
public abstract partial class BaseController : Controller
{
// GET: Base
protected override void Initialize(RequestContext requestContext)
{
try
{
//Checks if the user is logged in
if (requestContext.HttpContext.Session["customer"] != null)
{
ViewBag.Customer = requestContext.HttpContext.Session["customer"];
ViewBag.Points = requestContext.HttpContext.Session["points"];
ViewBag.CardNumber = requestContext.HttpContext.Session["cardNumber"];
}
//Gets the products to be displayed
var products = ProductList.Instance.AsQueryable();
ViewBag.Products = products;
//Checks to see if the user has a cart added to his requestContext.HttpContext.Session
if (requestContext.HttpContext.Session["cart"] == null)
{
requestContext.HttpContext.Session["cart"] = new Cart();
}
Cart cart = (Cart)requestContext.HttpContext.Session["cart"];
ViewBag.CartCount = cart.Count();
if (requestContext.HttpContext.Session["ticketID"] == null)
{
requestContext.HttpContext.Session["ticketID"] = Guid.NewGuid();
}
//Adds a cookie to the user with his selected theme
HttpCookie cookieUserTheme = requestContext.HttpContext.Request.Cookies["cookieUserTheme"];
if (cookieUserTheme != null)
{
requestContext.HttpContext.Session["UserPref"] = UserModel.GetThemeByName(cookieUserTheme.Value);
}
else
{
requestContext.HttpContext.Session["UserPref"] = UserModel.GetThemeByName("5");
var cookie = new HttpCookie("cookieUserTheme", ((UserPref)requestContext.HttpContext.Session["UserPref"]).ID);
cookie.Expires = DateTime.Now.AddDays(90);
requestContext.HttpContext.Response.Cookies.Add(cookie);
}
ViewBag.UserPref = requestContext.HttpContext.Session["UserPref"];
}
catch (Exception ex)
{
throw ex;
}
}
}
}
And this is the controller.
public class AdminController : BaseController
{
// GET: Admin
public ActionResult Index()
{
ViewBag.Themes = SiteMethods.GetAllThemes();
return View();
}
But when this is done running it just goes to the following ASP.NET page
What am i doing wrong? Do i need a redirect from my basecontroller?
If you're overriding Controller.Initialize() with your own initialization logic, you need to call base.Initialize(requestContext) to continue with the regular initialization process:
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
// rest of your code
// ...
}
Otherwise, this.ControllerContext (that is being used internally by several properties), would be null.
See Source

Access current Model in spring-mvc

I'm learning Spring-MVC 4 comming from asp.net MVC and I was looking for a way to pass data to the View without having to declare a Model Atrribute in every call.
For example, now I have this.
public class BaseController {
public void AddMessage(Model model, String m) {
Model.addAttribute("msg", m);
}
}
public class PersonController extends BaseController{
#RequestMapping("details/{Id}")
public String details(#PathVariable int Id, Model model) {
Person p = service.LoadById(Id);
if(p == null) {
AddMessage(model, "Record not found...");
} else {
model.addAttribute("bean", q);
}
return "person/details";
}
}
But what I would really like is to have a way to acess that Model instance in my base controller methods without having to pass it on as an argument. Similar to the usage of ViewData or TempData in asp.net MVC.
Is it possible to pass data to the view in this fashion?
Thank you
If you want to avoid passing the Model as a method parameter, you can use ModelAttribute annotation in a method:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ModelAttribute.html
Just annotate the method and Spring will automatically add what the method returns to the model.
#ModelAttribute
public Stuff addStuffToModel() {
Stuff stuff = new Stuff("dummy data");
return stuff; // stuff is added to the model
}
I managed to work around this issue using a request interceptor. Essentially:
On my base controller class:
public abstract class BaseController {
protected List<UserViewMessage> viewMessages;
public List<UserViewMessage> getViewMessages() {
if (viewMessages == null) {
viewMessages = new ArrayList<UserViewMessage>();
}
return viewMessages;
}
public void addMessage(String message, UserViewMessageType type) {
getViewMessages().add(new UserViewMessage(message, type));
}
public void clearMessages() {
if (viewMessages != null) {
viewMessages.clear();
}
}
}
Then, I added an interceptor to copy the messages collection to the Model:
public class RequestInterceptor extends HandlerInterceptorAdapter {
private static String MODEL_MESSAGES_KEY = "ModelMessageList_";
#Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (handler instanceof org.springframework.web.method.HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handlerMethod != null) {
Object bean = handlerMethod.getBean();
if (bean != null && bean instanceof BaseController) {
BaseController bc = (BaseController) bean;
bc.clearMessages();
}
}
}
return super.preHandle(request, response, handler);
}
#Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
if (handler instanceof org.springframework.web.method.HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handlerMethod != null && modelAndView != null) {
Object bean = handlerMethod.getBean();
if (bean != null && bean instanceof BaseController) {
BaseController bc = (BaseController) bean;
if (bc.getViewMessages() != null) {
modelAndView.addObject(MODEL_MESSAGES_KEY, bc.getViewMessages());
}
}
}
}
super.postHandle(request, response, handler, modelAndView);
}
}
Which, on PreHandle, clears any messages on the base controller collection. After the request (PostHandle) and since the Model is available, I copy the message collection to the Model, thus making it available on my views like so:
<div class="row">
<div class="col-lg-12">
<c:forEach var="messageItem" items="${_ModelMessageList_}">
<div class="alert alert-info"><c:out value="${messageItem.message}" /></div>
</c:forEach>
</div>
</div>
It's not optimal, but it works.

AuthorizeAttribute at asp.net MVC cannot write log to file

I try to customize AuthorizeAttribute for authentication at restful service. I want to write information into a log file. But it didn't work. I don't see any log file at my log path. (I have enable permission to IIS_USER)
Also, I even found that if AuthorizeCore return false, my test client can still get the result. Is somewhere in my code wrong? Or something I misunderstand how AuthorizeAttribute work?
PS. I also found that if I switch log part to ApiController it will work! It's so weird!
Filter
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
using (StreamWriter sw = new StreamWriter(#"C:\inetpub\wwwroot\mvctest\logs\Trace.log", true, Encoding.UTF8))
{
IEnumerable<String> header = httpContext.Request.Headers.GetValues("User");
foreach (String str in header)
{
sw.WriteLine(str);
}
sw.Flush();
}
return base.AuthorizeCore(httpContext);
}
}
ApiController
public class ValuesController : ApiController
{
// GET api/values
[MyAuthorizeAttribute]
public List<String> Get()
{
return new List<String> { "Success", "Get" };
}
// GET api/values/5
[MyAuthorizeAttribute]
public String Get(int id)
{
return "Success";
}
}
TestClient
static void Main(string[] args)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/mvctest/api/values/5");
request.Headers.Add("User", "Darkurvivor");
using (WebResponse response = request.GetResponse())
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
String data = sr.ReadToEnd();
Console.WriteLine(data);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
There are two AuthorizeAttributes. One for WebApi and one for MVC. WebApi uses System.Web.http.AuthorizeAttribute, while MVC uses System.Web.Mvc.AuthorizeAttribute. You canoot use one with the other. So, no. It's not weird at all.

The IControllerFactory 'MyWebSite.WebUI.Infrastructure.NinjectControllerFactory' did not return a controller for the name 'Admin'

I am getting the above when I try and open a view in a controller in an Area. Ninject is set up as follows:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel kernel = new StandardKernel(new RLSBCWebSiteServices());
protected override IController GetControllerInstance(RequestContext context, Type controllerType)
{
if (controllerType == null)
return null;
return (IController)kernel.Get(controllerType);
}
private class MyWebSiteServices : NinjectModule
{
public override void Load()
{
Bind<IMatchesRepository>().To<SqlMatchesRepository>().WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["MyWebSiteDb"].ConnectionString);
}
}
}
If I place a breakpoint in the code, I see the RequestContext context contains the following values:
context.RouteData.DataTokens.Values[0] = “MyWebSite.WebUI.Areas.Visitor” context.RouteData.DataTokens.Values[1] = “Visitor” which is the Area
context.RouteData.Values.Values[0] = “admin” which is the Controller
context.RouteData.Values.Values[1] = “register” which is the View
However controllerType == null, instead on the controller name.
This transfer to the new page is being triggered by
Html.ActionLink("here", "Register", "Admin", new { area = "Visitor" }, null)
which is on the Login page. However the same thing happens if I enter
http://example.com/Visitor/admin/register
into IE8
The area registration is as follows:
public class VisitorAreaRegistration : AreaRegistration
{
public override string AreaName { get { return "Visitor"; } }
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Visitor_default",
"Visitor/{controller}/{action}/{id}",
new { controller = "Admin", action = "Register", id = UrlParameter.Optional }
);
}
}
Has anyone managed to get Areas working with NinjectControllerFactory, or is there something wrong with my set-up?
Instead of creating your own NinjectControllerFactory use the latest version of Ninject.Web.Mvc. It supports Areas. See: https://github.com/ninject/ninject.web.mvc
Check your controller and action name in view. I also got same error as an action name was wrong.

Resources