razor: ignore httprequestvalidationexception - asp.net-mvc-3

I am using MVC3 Razor. I want my server to ignore the httprequestvalidation exception. I have tried
and all codes like [ValidateInput(false)]
none of these codes seem to work with MVC3 .net framework 4.0 razor

[ValidateInput(false)]
public ActionResult Index()
{
// ...method body
}
Using MVC 3, you must also ensure this is in your Web.config:
<system.web><httpRuntime requestValidationMode="2.0" /></system.web>

Related

Validation Firing on Page Load

Currently, I have an MVC 3 app using the Razor View engine. I have unobtrusive validation enabled. The problem is that for some reason, on page load, my Edit View is displaying errors for required fields (even though the fields have a value). Has anyone else ran into this? Any suggestions for resolving this? Thanks.
Sample Field with problem:
<div class="full">
<label>Description:</label>
#Html.EditorFor(x=>x.Description, new{#class="super-textarea"})
#Html.ValidationMessageFor(x => x.Description)
</div>
Data Annotations on Model:
[Required, DataType(DataType.MultilineText)]
public virtual string Description { get; set; }
WebConfig enabled settings:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
And of course the proper jquery files....
You can also clear the errors from the ModelState
ModelState.Clear();
Ok. Found the problem. Validation was happening due to Model binding attempting to take place. This was happening because our Get Method looks like this.
[HttpGet, RequestedObjectFilter]
public virtual ViewResult Edit(TKey id, T requestedObject)
{
return View(requestedObject);
}
A feature of .NET MVC is that anytime a reference value is passed as a parameter in the Method Signature of a ViewResult, ModelBinding is triggered, which in turn fires off validation. The reason that we were passing in the object to our method was due to our RequestedObjectFilter which would fetch the related entity from our abstracted repository, and pass it in to this method via the ActionParameters property. We refactored our RequestedObjectFilter to set the ViewModel instead, allowing us to remove the parameter from the method, thus solving the problem. Now our method looks like this:
[HttpGet, RequestedObjectFilter]
public virtual ViewResult Edit(TKey id)
{
return View();
}

ValidateRequest in Razor syntax

I have the following header of ASP.Net MVC page:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<NEOGOV_Ideas.Models.SubIdeaAdminPage>"
ValidateRequest="false" %>
I need to move this page to Razor syntax. How should I set ValidateRequest?
Thanks
Decorate your action method with ValidateInput attribute
[HttpPost]
[ValidateInput(false)]
public ActionResult index()
{
return view();
}
You shouldn't need that line in the view, instead use the ValidateInput(false) attribute on the controller method.
Make sure you've got this in your web.config if you're using ASP .net 4.0 (which I presume you are if you're using MVC 3)
<httpRuntime requestValidationMode="2.0"/>
Martin
From MVC 4 we can allow html content only for property of model class, not for the whole request. Just need to mark property by attribute AllowHtml
public class EditorialPixlocateRequestViewModel
{
[AllowHtml]
public string Xml { get; set; }
}

Using handleError out of the box in mvc3, problem

I trying to do something like that
First, Create a new mvc 3 project in visual studio 2010
Next, Turning on the custom error in the Views\Shared\Web.config
<system.web>
<customErrors mode="On"/>
...
And then, I put the Tag in the Index ActionResult, Home Controller
Public Class HomeController
Inherits System.Web.Mvc.Controller
<HandleError()> _
Function Index() As ActionResult
ViewData("Message") = "Welcome to ASP.NET MVC!"
Throw New InvalidOperationException
Return View()
End Function
Function About() As ActionResult
Return View()
End Function
End Class
Finally run the app, and always show the yellow message error. I review a lot of examples and always indicated that is correct, but doesn't work.
I appreciate your help
You should do this in the main ~/web.config file, not the one in ~/Views/Shared/Web.config:
<system.web>
<customErrors mode="On"/>
...
</system.web>
Also ensure that ~/Views/Shared/Error.cshtml is present as this will be the rendered view in case of an exception.
And you no longer need to decorate your controller with the <HandleError()> attribute as ASP.NET MVC 3 uses a global filter for this.

Using Page.User.Identity.Name in MVC3

In MVC2 I have used Page.User.Identity.Name using the <%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
How can I use the same in MVC3?
You can always do something like:
#Html.ViewContext.HttpContext.User.Identity.Name
but don't.
Normally a view shouldn't try to fetch such information. It is there to display whatever information is passed by the controller. It should be strongly typed to a model class which is passed by a controller action.
So in the controller action rendering this view:
[Authorize]
public ActionResult Index()
{
var model = new MyViewModel
{
Username = User.Identity.Name
}
return View(model);
}
Now inside the view feel free to use this information:
#Model.Username
MVC 2
<%: this.Page.User.Identity.Name %>
MVC 3
#this.User.Identity.Name
I had the same problem. I used this tutorial to solve this issue.
In short, in your view, put this code:
#Context.User.Identity.Name
Just make sure that the project is set to authenticate with windows.

How can I databind in the _Layout.cshtml

In ASP.NET WebForms, I can use the CodeBehind of a master page to fetch data to use to bind up my navigation.
How can I achieve the same in ASP.NET MVC 3?
Ideally the main navigation would be in the _Layout.cshtml but this file doesn't have it's own model. i.e. It can only use the model supplied by the controller action (assuming a base class and #model directive in the _Layout.cshtml.
Edit
While I realise MVC does not have the notion of DataBinding, I included it here to help describe the functionality I'm looking for.
How can I achieve the same in ASP.NET MVC 3?
The notion of databinding is not common for the MVC pattern. To implement the navigation you could use Html.Action and Html.RenderAction.
Example:
public class NavigationController : Controller
{
public ActionResult Index()
{
NavigationViewModel model = ...
return View(model);
}
}
and then inside the layout:
#Html.Action("Index", "Navigation")
The index.cshtml could be a partial which implements the navigation.

Resources