Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I'm really hoping you are able to help me with this as I am at a bit of a loss.
I have inherited some code which I have been asked to modify to allow the submission of HTML formatted values. However, each time the method fires it gets so far than throws a "HTTPRequestValidationException" when it tries to retrieve said html formatted text.

This is the action code:

C#
[ValidateInput(false)]
public ActionResult SaveCustomContent()
{
     string type = Request["type"];
     bool found = false;
     Type actualType = null;
     object actualObject = null;

     foreach (
        Type t in
           from asm in AppDomain.CurrentDomain.GetAssemblies()
              from t in asm.GetTypes()
                where t.Name == type
                  select t)
     {
         found = true;
         actualType = t;
         actualObject = Activator.CreateInstance(t);
     }

     if (found && actualType != null)
     {
         // Populate the properties
         foreach (PropertyInfo pi in actualType.GetProperties())
         {
             if (Request[pi.Name] != null) //**ERRROR THROWS HERE**//
             {
                 try
                 {
                      pi.SetValue(actualObject, Request[pi.Name], null);
                 }
                 catch
                 {
                      try
                      {
                           pi.SetValue(actualObject, Convert.ChangeType(Request[pi.Name], pi.PropertyType), null);
                       }
                       catch
                       {
                       }
                 }
             }
          }


The method does continue to do other things which its why it cuts off abruptly. I have marked the line throwing the exception with //**ERRROR THROWS HERE**// So that it is easy to spot.

As you can see I've marked the the method with the "ValidateInput(false) attribute, in addition the model that is used for the view, which in turn submits data back here has had the appropriate property decorated with the [AllowHtml] attribute.

Any ideas what I am missing or what I should do to overcome this issue?

Bare in mind, I didn't originally write this action, and in my personal opinion it seems like a stupid approach. unfortunately this needs making available asap, and for me to completely re-work it into a more sensible approach isn't realistic at this time.

I hope you can help.
Posted
Comments
Karthik_Mahalingam 26-Nov-13 4:56am    
Hi
can u add this line
if( Request.AcceptTypes.Contains(pi.Name))
instead of
if (Request[pi.Name] != null) //**ERRROR THROWS HERE**//
Pheonyx 26-Nov-13 5:27am    
Doing that stops the error, but it results in no data being retrieved, in turn it does not save the data to the database (which is the code I missed out from the bottom of the function).
Karthik_Mahalingam 26-Nov-13 5:37am    
remove this bracket }
above to this line.
if (found && actualType != null)

and add } at the end of the function and try..
Pheonyx 26-Nov-13 5:42am    
Why? I don't see how that would make any difference with regards to the error that is being thrown. (Sorry, I'd just like an explanation as to why you are suggesting this please)
Karthik_Mahalingam 26-Nov-13 5:49am    
{ found = true;
actualType = t;
actualObject = Activator.CreateInstance(t);
}

the above code area will be initialised with the last index value of the collections result from the linq query..
so there is no use of the loop. instead, use can use the last value form the collection..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900