Asp.Net MVC 4 - Load two pages in same time - asp.net-mvc

I have a little problem with this part of code.
I want to load two pages without lock between them.
First page has a wait task (25sec).
Second has no wait tasks.
When I run my web app and call this two pages, I have to wait the end of first page to get the second page.
My question, how to load these two pages fully asynchronously?
public class AsyncTestController : Controller
{
// GET: AsyncTest
public async Task<ActionResult> Delay()
{
await Task.Run(() => Thread.Sleep(25000));
return View();
}
// GET: AsyncTest
public async Task<ActionResult> Index()
{
return View();
}
}

Related

Swagger not loading - Failed to load API definition: error undefined message

As I tried to change one public method to private. The issue will be gone.
But when I defined the public method on both method as the below controller code, it will bring up the error.
Anyone help/advice if possible would be really appreciate
WEBLABController.cs
[Route("api/SearchLABResults")]
[HttpPost]
public async Task<IActionResult> SearchLABResults([FromBody] SP_GET_LABResult_Overview.input data)
{
IActionResult response = Unauthorized();
......
return response;
}
AuthenticationController.cs
[Route("api/GenerateToken")]
[HttpPost]
public async Task<IActionResult> GenerateToken([FromBody] AuthenticationModel.input data)
{
IActionResult response = Unauthorized();
......
return response;
}
If you scroll down the swagger view, You see that every model you are using is shown there. Swagger needs that those names be unique.
SP_GET_LABResult_Overview.input and AuthenticationModel.input are both generate input schema and that cause InvalidOperationException.
Try to change one of the input classes to something else or change their schema on the fly like this.
services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
});

Async HttpPost requests - ASP.NET MVC 5

I'm trying to make an async HttpPost requests with Ajax & MVC.
I know that SessionId is the problem: multiple requests from the same client with the same SessionId will add the second request to the queue.
after I google it I saw some answers for this it fails to me.
I tried to clear session data.
I tried to add an mvc attribute to change session mode to readOnly:
[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class TestController : SurfaceController
{
[HttpPost]
public ActionResult Test1()
{
Thread.Sleep(5000);
return Json("1");
}
[HttpPost]
public ActionResult Test2()
{
Thread.Sleep(5000);
return Json("2");
}
}

MVC: trying to understand where ActionResult sends it's data

I have been trying to understand an online tutorial and I am stumped.
Can someone please tell me where the text "Hello" is sent to? Is the message sent directly to the browser without being placed on a page?
public class GoHomeController : Controller
{
public string Index()
{
return "Hello";
}
}
How's this? Your controller action needs to have a return type of ActionResult, there are many subclasses of this class that allow for various types of responses however you can always influence with brute force if you like. For example"
public ActionResult Index()
{
Response.Write("hello world");
return null;
}
The above code writes to the Response stream directly, in my example I return a null. This indicates no ActionResult is needed to be performed by the MVC system, typically this is where the View is specified, the View will be read, parsed and written to the Response stream as well.
But typical controller actions do have return values, for example here is how I could return JSON, remember the View is just an abstraction to allow you to control what is written to the Response stream.
public ActionResult Index()
{
return Json( new { Message="Hello world"});
}
And then there is the typical ActionResult that directs the output to a .cshtml file:
public ActionResult Index()
{
return View();
}
This will write to the Response stream using the Index.cshtml file tied to this controller namespace or I could specify the name of the .cshtml:
public ActionResult Index()
{
return View("HelloWorld"); //<-- looks for HelloWorld.cshtml
}

Performing an async operation before every action in a controller

Consider the following:
public class FooController : Controller
{
public async Task<ActionResult> Foo()
{
await DoSomethingAsync();
...
return View();
}
public async Task<ActionResult> Bar()
{
await DoSomethingAsync();
...
return View();
}
...
}
This line, await DoSomethingAsync(); is repeated for each action in the controller. It'd be much nicer to do something like:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
await DoSomethingAsync();
}
But, obviously, you can't run an async op in a synchronous method. I could simply force the async op to run sync, but then you end up with the thread being blocked unnecessarily. Short of running this sync, what options do I have?
Unfortunately, ASP.NET MVC does not have asynchronous filters today. ASP.NET WebAPI does have them, and ASP.NET vNext (combining MVC and WebAPI) does have them.
So, for today I'd say you're best off repeating the code, but in the future (near future, hopefully) that could be cleaned up.

MVC 4 Routing , Get/Post requests handling

I'm faced with the following problem :
I have a controller with lets say the following actions:
[HttpGet]
public ActionResult Index()
{
var viewModel = new IndexViewModel();
return View("Index", viewModel);
}
[HttpPost]
public void ExportToExcell(LeadsViewModel model)
{
// Export to excell code goes here
}
The problem is the following:
The User enters on Index page with this URL : /Controller/Index
Then the user submits the form to Action ExportToExcel
Data is exported to Excel( file downloaded ) and it's okay.
The URL becomes /Controller/ExportToExcell
Then when I am clicking "Enter" I am going To /Controller/ExportToExcell but with GET
and of course falling with Page Not Found, the question is how properly to Deal with this in MVC
Don't use void as returned type of your post action, use an ActionResult
[HttpPost]
public ActionResult ExportToExcell(LeadsViewModel model)
{
// Export to excell code goes here
return RedirectToAction("Index");
}
I believe that your problem is that you aren't returning a FileResult, and the browser will redirect you to your post path. Can't test it right now, but I believe the following should work.
[HttpPost]
public ActionResult ExportToExcell(LeadsViewModel model)
{
// Generate the Excel file into a MemoryStream for example
// Return a FileResult with the Excel mime type
return File(fileStream, "application/vnd.ms-excel", "MyExcelFile.xls");
}
Check FileResult and Controller.File for more details.
As a note, I'm not completely sure if that's the mime type for an Excel file, but if you say you are already downloading the file, your probably already have it :)
You must return ActionResult instead of void.
public ActionResult ExportToExcel(PagingParams args)
{
var data = new StudentDataContext().Student.Take(200).ToList();
return data.GridExportToExcel<Student>("GridExcel.xlsx", ExcelVersion.Excel2007, args.ExportOption);
}
Please check the link: Export Action

Resources