controller-action

How to unit test whether a Core MVC controller action calls ControllerBase.Problem()

こ雲淡風輕ζ 提交于 2020-05-09 07:39:26
问题 We have a controller that derives from ControllerBase with an action like this: public async Task<ActionResult> Get(int id) { try { // Logic return Ok(someReturnValue); } catch { return Problem(); } } We also have a unit test like this: [TestMethod] public async Task GetCallsProblemOnInvalidId() { var result = sut.Get(someInvalidId); } But ControllerBase.Problem() throws a Null Reference Exception. This is a method from the Core MVC framework, so I don't realy know why it is throwing the

Starting and Forgetting an Async task in MVC Action

落花浮王杯 提交于 2020-01-22 12:26:25
问题 I have a standard, non-async action like: [HttpPost] public JsonResult StartGeneratePdf(int id) { PdfGenerator.Current.GenerateAsync(id); return Json(null); } The idea being that I know this PDF generation could take a long time, so I just start the task and return, not caring about the result of the async operation. In a default ASP.Net MVC 4 app this gives me this nice exception: System.InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous

Using a .NET MVC Controller Action as the Source for an HTML <img>

谁说胖子不能爱 提交于 2020-01-01 16:43:26
问题 I'm trying to display the picture associated with a user in my database (the picture field's data type is image ) on a page - unfortunately the code below fails to do that. HTML <img src="/User/Picture/1" /> Controller Action public byte[] Picture(int id){ UserRepository r = new UserRepository(); return r.Single(id).logo.ToArray(); } 回答1: PROBLEM SOLVED Apologies, I didn't read up enough on this! All that needed to be done was make the Controller Action return FileContentResult public

How to test a controller action that does not exist?

雨燕双飞 提交于 2019-12-11 09:15:03
问题 There are only two actions accessible in the ProductsController : # /config/routes.rb RailsApp::Application.routes.draw do resources :products, only: [:index, :show] end Tests are set up accordingly: # /spec/controllers/products_controller_spec.rb require 'spec_helper' describe ProductsController do before do @product = Product.gen end describe "GET index" do it "renders the index template" do get :index expect(response.status).to eq(200) expect(response).to render_template(:index) end end

ASP.NET MVC 4: Only allow one request at a time

家住魔仙堡 提交于 2019-12-11 02:46:52
问题 In my ASP.NET MVC Application, I want to handle all requests sequentially; no action/controller code should be executed concurrently with another. If two requests come in at similar times, it should run the first one first, then the second one when the first one is done. Is there a better way of doing this besides using a global lock variable? EDIT: The application is more of a batch/service over the web that performs web service calls and cleans up a database. Different URLS in the site lead

Html.Action - Get versus Post

寵の児 提交于 2019-12-04 16:45:22
问题 I do this very often: <div id='Product'> @Html.Action("Create", "Product") </div> it's convenient because I can delegate the painting of a product creation form to another controller action for embedding in places. However, I'm having issues in that the method will sometimes (I haven't figured out under what conditions) call the [HttpPost] of my controller action, which of course fails. Is there a way to force @Html.Action() to call the GET version? 回答1: The way Html.Action works is that if

Using a .NET MVC Controller Action as the Source for an HTML <img>

余生长醉 提交于 2019-12-04 14:28:43
I'm trying to display the picture associated with a user in my database (the picture field's data type is image ) on a page - unfortunately the code below fails to do that. HTML <img src="/User/Picture/1" /> Controller Action public byte[] Picture(int id){ UserRepository r = new UserRepository(); return r.Single(id).logo.ToArray(); } Jimbo PROBLEM SOLVED Apologies, I didn't read up enough on this! All that needed to be done was make the Controller Action return FileContentResult public FileContentResult Picture(int id) { UserRepository r = new UserRepository(); return new FileContentResult(r

Enforce Action Filter on all Controller Actions (C# / ASP.NET MVC)

旧时模样 提交于 2019-12-03 16:39:00
问题 I made a new action filter (attribute, similar to [Authorize]) which authorizes access to a controller action based on a session value. However, I'm basically decorating all my controller actions with that attribute (with the exception of very few). So, I thought it would be better to have that Action Filter always executed except in cases where I attach an [ExemptFromAuthorize] attribute to a controller action? (Maybe via inheriting to my own Controller class?) How can I do this? 回答1:

Html.Action - Get versus Post

﹥>﹥吖頭↗ 提交于 2019-12-03 09:58:54
I do this very often: <div id='Product'> @Html.Action("Create", "Product") </div> it's convenient because I can delegate the painting of a product creation form to another controller action for embedding in places. However, I'm having issues in that the method will sometimes (I haven't figured out under what conditions) call the [HttpPost] of my controller action, which of course fails. Is there a way to force @Html.Action() to call the GET version? The way Html.Action works is that if the current request for the page is a post method then it will search for the method with the name HttpPost .

Starting and Forgetting an Async task in MVC Action

我的未来我决定 提交于 2019-12-03 07:29:26
I have a standard, non-async action like: [HttpPost] public JsonResult StartGeneratePdf(int id) { PdfGenerator.Current.GenerateAsync(id); return Json(null); } The idea being that I know this PDF generation could take a long time, so I just start the task and return, not caring about the result of the async operation. In a default ASP.Net MVC 4 app this gives me this nice exception: System.InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page