MVC controller action is called twice
On IIS 7 server with my ASP.NET MVC 3 application, I am encountering the following problem: when I have an action taking rather long time to complete (like 30 seconds), it will get fired for the second time after cca 15-20 seconds from the first request. I can see in Fiddler (HTTP sniffer) that the browser is not the culprit - it only sends the request once. However, in IIS log files, I can see the request twice. However, in IIS logs, both requests have the same timestamp (not in my own log files - there the two requests are separated by those 15-20 seconds).
Originally, the action was processing an uploaded file, storing it in the database etc..However, even after I changed the action to just call Thread.Sleep(30000)
, it is still called twice.
It is not caused by some malfunctioning JavaScript or missing-image references which seem to be the common reasons for this behavior as I read through similar problems here on StackOverflow. I cannot replicate this on the development ASP.NET server, only on the IIS server and just on one of two I use.
This is the HTML form used to trigger that action
@model TMAppServer.Abstract.DataTransferObject.ProjectOverviewData
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>UploadTestForm</title>
</head>
<body>
<div>
@using (Html.BeginForm("UploadFileNew", "Manager", new { targetLanguage = "CS" }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input id="files" type="file" name="files" /><text><input type="submit" id="uploadFile" value="Upload the document..." /><input type="hidden" name="projectId" id="projectId" value="@(Model.Job.Name)" />
}
</div>
</body>
</html>
This is the controller action:
public ActionResult UploadFileNew(string projectId, string targetLanguage, IList<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (null == file)
continue;
Thread.Sleep(30000);
}
return RedirectToAction("GetProjectOverview", new { projectId = projectId });
}
Thank you for any suggestions.
EDIT: Now I found out that this only happens when I access the server via its domain name from the same network (like server.domain.com), it does NOT happen when accessing the server via its IP address or from an outside network.
This is what I get in the IIS logs:
2011-07-21 01:23:31 W3SVC2 WIN-AD50B4LJ2SU 192.168.1.48 POST /manager/upload-file projectId=3366 80 - 192.168.1.1 HTTP/1.1 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+rv:5.0.1)+Gecko/20100101+Firefox/5.0.1 .ASPXAUTH=cookie http://tm.web.com/manager/projects/3366/overview tm.web.com 302 0 0 440 6232309 578
2011-07-21 01:23:31 W3SVC2 WIN-AD50B4LJ2SU 192.168.1.48 GET /manager/projects/3366/overview - 80 - 192.168.1.1 HTTP/1.1 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+rv:5.0.1)+Gecko/2010010开发者_StackOverflow1+Firefox/5.0.1 .ASPXAUTH=cookie http://tm.web.com/manager/projects/3366/overview tm.web.com 200 0 0 30125 769 93
2011-07-21 01:23:31 W3SVC2 WIN-AD50B4LJ2SU 192.168.1.48 POST /manager/upload-file projectId=3366 80 - 192.168.1.1 HTTP/1.1 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+rv:5.0.1)+Gecko/20100101+Firefox/5.0.1 .ASPXAUTH=cookie http://tm.web.com/manager/projects/3366/overview tm.web.com 302 0 64 0 6232309 39406
And these are my routes in Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,
"manager/upload-file",
new { controller = "Manager", action = "UploadFile" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(null,
"manager/projects/{projectId}/overview",
new { controller = "Manager", action = "GetProjectOverview" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(null,
"manager/{action}",
new { controller = "Manager", action = "Main" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Manager", action = "ListProjects", id = UrlParameter.Optional } // Parameter defaults
);
}
From the logs you posted it looks like your web server is returning HTTP302 which is 'Moved Temporarily' or as IIS implements it 'Object Moved' status. It sounds like a network configuration problem.
Does it happen to every action or only that one?
Is there any other markup that could be accidentally referencing the page? Script references, image references, css references, all could be mistakenly recalling the current page. MVC is known for doing this even from minuscule errors, like incorrect image paths.
Not returning false from a javascript click handler on a link could also cause this. Probably not your issue since I see no js in your code here, but figured I'd mention it in case you have some hiding in your masterpage maybe?? it can trigger the default action of the link, ex:
<%= Html.ActionLink( "action", "controller" ) %>
$(function() {
$('a').click( function() {
var href = $(this).attr('href');
$.get( href, function() { ... } );
// needs "return false;" here to prevent double request
});
}):
Is it possible to recreate this error while in debug mode? That would be prime for walking through each request step by step. If not, i'd run Fiddler to examine each request, you might get lucky with that. Or you could even use Application_BeginRequest event of the Global.asax to log specific information about each request.
精彩评论