Why did not redirect to view in ASP.NET MVC 4

2019-02-15 23:10发布

问题:

All, I have a controller which saved the posted file(Please review below code). It took about 20-30 min to finish it. But I found after saving the file, the RedirectToAction didn't work. the IE status bar shown :

Waiting for http://......./index.

BTW, My session state is stored in SQL Server. And timeout is 300 Mins. I am not sure this problem if has relationship with IIS 7 app pool idle time which I post here before. Although it didn't redirect to the action ,I found the session still exists. The file and the record were saved successfully .Only problem is It didn't redirect. please help me .thanks.

[HttpPost]
[AcceptButton("Upload")]
public ActionResult Upload(UploadPackageModel model)
{
    //after about 20-30 min for 160MB file-upload, runs here.
    DeployLogModel tempLog = null;
    try
    {
        if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
        {
            var file = Request.Files[0];
            var fileName = Path.GetFileName(file.FileName);
            string sUploadFullPath = string.Empty;
            model.ID = Guid.NewGuid();//PK id of Uploaded Package
            string sUploadFileName = model.ID + Path.GetExtension(file.FileName);
            model.CreatedBy = DataHelp.LoginAdministrator.ID.ToString();
            model.LastUpdatedBy = DataHelp.LoginAdministrator.ID.ToString();
            model.PackageSize = Request.Files[0].ContentLength;
            model.CreatedDate = DateTime.UtcNow;
            model.LastUpdatedDate = DateTime.UtcNow;
            model.PackageName = fileName;

            string rootPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + "DeployPackages\\";

            sUploadFullPath = Path.Combine(rootPath, sUploadFileName);
            model.PackagePath = sUploadFullPath;
            if (!Directory.Exists(rootPath))
                Directory.CreateDirectory(rootPath);

            file.SaveAs(sUploadFullPath);//Save the uploaded package.
            model.SavePackInfoIntoDB(model);//Save record to DB
        }
    }
    catch (Exception ex)
    {
        Log.Write("-Upload-Package-failed-:\r\n" + ex.Message + "\r\n" + ex.StackTrace);
    }
    return RedirectToAction("Index");//does not work.
}

The file and the record were saved successfully .Only problem is It didn't redirect.

Update:

Web.config httpRuntime setting is:

<httpRuntime executionTimeout="14400" maxRequestLength="716800" />

Edited to add Index action code:

public ActionResult Index(int? page)
{
        var db = DbContextHelper.CreateInstance();

        //Find all uploaded packages.
        var packages = db.DeployPackages.OrderByDescending(x => x.CreatedDate).Select(x => new PackageModel
             {
                 PackageId = x.ID,
                 UploadedBy = x.CreatedBy,
                 Description = x.Description,
                 PackageName = x.PackageName,
                 PackageSize = x.PackageSize,
                 PackagePath = x.PackagePath,
                 UploadTime = x.CreatedDate,
                 VersionName = x.VersionName
             }).ToList();

        int pageSize = ConfigHelper.BigPageSizeNum;
        int pageNumber = (page ?? 1);
        return View(packages.ToPagedList(pageNumber, pageSize));
    }

Edited to add Fiddler trace info

result protocol Host                 Url          

200    http     test.cloudapp.net    /package/upload
200    http     test.cloudapp.net    /package/index

I can see the response html content in the fiddler for /package/index,but the browser just freeze on the same page(/package/upload) .

I wondered if there is a way to change the windows.location.href with js code ,My idea is when finished upload action successfully. then output to client some script like response.write script in classical asp.net page . the js change windows.location.href to redirect to /package/index. Is there any way to make it in ASP.NET MVC4?

回答1:

Using Content(sResultScript, "text/html") to output javascript to redirect index url. IE8 shown a failed page , chrome and Firefox is OK .