You need to check to see if ModelState.IsValid before you attempt to check Validation.IsValid()
@{
Page.Title = "Add Game";
var GameName = "";
Validation.Add("Name",
Validator.Required("Please give the game a name"),
Validator.Regex(@"^[A-Za-z0-9 _]*$", "The name must only contain letters, numbers, space and/or underscores."),
Validator.StringLength(
maxLength: 100,
errorMessage: "Name must be less than 100 characters")
);
try{
if (IsPost && Request.Files[0].ContentLength == 0){
ModelState.AddError("file", "You must choose a file");
}
if (IsPost && Request.Files[0].ContentLength > 0){
if (!Path.GetExtension(Request.Files[0].FileName.ToLower()).Equals(".swf")){
ModelState.AddError("file", "Only Flash files allowed");
}
}
}
catch (HttpException ex){
ModelState.AddError("file", "File exceeds 10MB");
}
if(ModelState.IsValid){
if (Validation.IsValid()){
var db = Database.Open("Surgestuff");
GameName = Request["Name"];
var gCat = "";
var fileData = Request.Files[0];
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileData.FileName);
var fileSavePath = Server.MapPath("~/upload/" + fileName);
var AddBy = WebSecurity.CurrentUserName; var mod = 0;
var count = 0;
gCat = Request["formCat"];
var Gamefile = fileName;
fileData.SaveAs(fileSavePath);
var SQLINSERT = "INSERT INTO Games (Name, file_path, Category, AddBy, Mod, Hits) VALUES (@0, @1, @2, @3, @4, @5)";
db.Execute(SQLINSERT, GameName, Gamefile, gCat, AddBy, mod, count);
Response.Redirect("~/Games");
}
}
}
I'm not currently at my computer but if I remember correctly the exception was caused by 'value=@GameName' so I then got rid of var GameName = ""; from the beginning of my razor block then changed it the bit inside my try block to say var GameName = Request["Name"];
and changed my input to say 'value=@Request["Name"]' and tested with a 13mb swf file. I then got the exception with the line that says @Html.ValidationMessage("File")
*Update*
Now that I'm on my computer, I kept my html markup exactly the same and then copied the code that you posted, tested the 13mb .swf file again and I still get the
HttpException: Maximum request length exceeded.
My multimedia major project: http://www.surgestuff.com | Please mark me as answer if I helped
It seems that the validation helpers are just not compatible with potential Request Length Exceeded exceptions. Any time that you reference the Request object, the HttpException is thrown. The validators reference it as part of their internals. For the time
being, I'd suggest setting up global error handling to trap this particular exception in a _PageStart file.
Incidentally, if you try to upload a file that exceeds 27MB, the error doesn't even get as far as ASP.NET. IIS raises errors instead.
Mikesdotnett...
All-Star
154941 Points
19870 Posts
Moderator
MVP
Re: Filetype validation regex not working
Dec 22, 2012 02:52 PM|LINK
You need to check to see if ModelState.IsValid before you attempt to check Validation.IsValid()
@{ Page.Title = "Add Game"; var GameName = ""; Validation.Add("Name", Validator.Required("Please give the game a name"), Validator.Regex(@"^[A-Za-z0-9 _]*$", "The name must only contain letters, numbers, space and/or underscores."), Validator.StringLength( maxLength: 100, errorMessage: "Name must be less than 100 characters") ); try{ if (IsPost && Request.Files[0].ContentLength == 0){ ModelState.AddError("file", "You must choose a file"); } if (IsPost && Request.Files[0].ContentLength > 0){ if (!Path.GetExtension(Request.Files[0].FileName.ToLower()).Equals(".swf")){ ModelState.AddError("file", "Only Flash files allowed"); } } } catch (HttpException ex){ ModelState.AddError("file", "File exceeds 10MB"); } if(ModelState.IsValid){ if (Validation.IsValid()){ var db = Database.Open("Surgestuff"); GameName = Request["Name"]; var gCat = ""; var fileData = Request.Files[0]; var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileData.FileName); var fileSavePath = Server.MapPath("~/upload/" + fileName); var AddBy = WebSecurity.CurrentUserName; var mod = 0; var count = 0; gCat = Request["formCat"]; var Gamefile = fileName; fileData.SaveAs(fileSavePath); var SQLINSERT = "INSERT INTO Games (Name, file_path, Category, AddBy, Mod, Hits) VALUES (@0, @1, @2, @3, @4, @5)"; db.Execute(SQLINSERT, GameName, Gamefile, gCat, AddBy, mod, count); Response.Redirect("~/Games"); } } }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
mhcodner
Member
219 Points
86 Posts
Re: Filetype validation regex not working
Dec 22, 2012 11:15 PM|LINK
Sorry but I still get the yellow screen with max request length exceeded
Mikesdotnett...
All-Star
154941 Points
19870 Posts
Moderator
MVP
Re: Filetype validation regex not working
Dec 23, 2012 06:33 AM|LINK
I don't understand why. I tested the code I posted and it works for me. What line is causing the exception?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
mhcodner
Member
219 Points
86 Posts
Re: Filetype validation regex not working
Dec 23, 2012 07:37 AM|LINK
I'm not currently at my computer but if I remember correctly the exception was caused by 'value=@GameName' so I then got rid of var GameName = ""; from the beginning of my razor block then changed it the bit inside my try block to say var GameName = Request["Name"]; and changed my input to say 'value=@Request["Name"]' and tested with a 13mb swf file. I then got the exception with the line that says @Html.ValidationMessage("File")
*Update*
Now that I'm on my computer, I kept my html markup exactly the same and then copied the code that you posted, tested the 13mb .swf file again and I still get the HttpException: Maximum request length exceeded.
Mikesdotnett...
All-Star
154941 Points
19870 Posts
Moderator
MVP
Re: Filetype validation regex not working
Dec 23, 2012 09:32 PM|LINK
It seems that the validation helpers are just not compatible with potential Request Length Exceeded exceptions. Any time that you reference the Request object, the HttpException is thrown. The validators reference it as part of their internals. For the time being, I'd suggest setting up global error handling to trap this particular exception in a _PageStart file.
Incidentally, if you try to upload a file that exceeds 27MB, the error doesn't even get as far as ASP.NET. IIS raises errors instead.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
mhcodner
Member
219 Points
86 Posts
Re: Filetype validation regex not working
Dec 24, 2012 12:04 AM|LINK
OK, thank you. Even though it is not what I would like to achieve, it is better then presenting users with a yellow screen.