NotSupportedException: Ambiguous HTTP method for action - AAON.Insiite.Api.Controllers.TokenController.Get (AAON.Insiite.Api). Actions require an explicit HttpMethod binding for Swagger 2.0
I've read the several questions regarding this issue on SO but none of the solutions have worked.
I've tried:
Adding [HttpGet] to the method in question.
Adding [NonAction] to the method in question.
Changing access modifier of method to Protected.
Deleting the method still throws an exception for the now deleted method.
Cleared Visual Studio Cache
I've tried clearing my visual studio cache but to no avail.
[HttpGet]
public IActionResult Get()
{
return Ok("Running");
}
According to #Crumblenautjs answer, I had a similar problem with the custom routing convention. After debugging for a while I discovered that also RouteAnalyzer_Main (which is used by swagger to define routes) routes are being replaced together with "my" controllers.
My solution is to skip Apply when RouteAnalyzer occurs, at least works for my solution.
public void Apply(ControllerModel controller)
{
if (controller.ControllerName == "RouteAnalyzer_Main")
{
return;
}
}
The issue was a controller naming convention. Solved.
Related
The configuration method below doesn't get fired.
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(SCM.Web.Startup))]
namespace SCM.Web
{
public partial class Startup
{
public void Configuration(IAppBuilder builder) { }
}
}
I've followed all the hints from here and it's a new project, not an upgrade. I can't for my life see how to get it to stop on the breakpoint and I'm in need to get more suggestions on how to troubleshoot it.
It's an intranet application so there's no logging in. The identity gets set to the Windows credentials, instead. I need to assign the roles so that only certain users can access certain actions in the controllers. I'm usually applying OWIN and application cookies, so that's the method I'm trying to follow here, as well.
You need an OwinStartup attribute to tell Owin what method to call. From the docs:
Used to mark which class in an assembly should be used for automatic startup.
Add one to your project, before the namespace declaration:
[assembly: OwinStartup(typeof(Your.Namespace.Startup))]
namespace Your.Namespace
{
public partial class Startup
{
public void Configuration(IAppBuilder builder) { }
}
}
There are some other methods to let Owin know which method (described here) but this is the simplest and probably the most common.
If you are running the website on an external IIS or maybe on the "real" IIS installed on your computer (and not the one that is fired up when you start the run), then it's likely that you're missing the breakpoint because the debugger isn't attached to the process yet when you pass by.
I think that you can confirm it by either checking the settings of your solution and projects or simply adding this code to the method that you don't think that you pass through.
throw new Exception("Killroy was here...");
I am calling my WebApi server methods from a console client and most of the calls work.
Some don't.
My routing seems fine but obviously there is a bug.
Is there a way to set a breakpoint somewhere ( earlier than my controller method ) which will allow me to trace the source of the error.
You must have two methods in the controller that is being called that are 'identical' to the routing engine. eg
public class FooController
{
public void Get1() { }
public void Get2() { }
}
if you attempt to call somedomain.com/api/foo you will get a 500 server error
Multiple actions were found that match the request: \r\nSystem.Net.Http.HttpResponseMessage
Remember that in WebApi (by default) there is no {action} part of the route:
{controller}/{id}
meaning the API has a convention for which method is called.
See this article explaining more
I'm running into an error with T4MVC and named parameters. I have a controller:
public class ProductsController : Controller
{
public virtual ViewResult List(int page = 1)
{
// foo.DoSomething()
}
}
It seems T4MVC creates an overload List() as well. The result is that calling
myProductsController.List(3)
correctly executes foo.DoSomething(). But calling
myProductsController.List()
does NOT execute foo.DoSomething() - T4MVC created an empty List() overload.
I've taken T4MVC out of my project, and everything works fine now. But I'd really like to be able to use it - am I missing a setting somewhere?
UPDATE: Ok, I have a real fix now. It's checked into the Codeplex repository. You can get the latest T4MVC.tt by going to here. Before I include that in the next official build, it would be great if you could try it and confirm that it works for you. Thanks!
You're right, there is a problem here. I had not run into this situation before. For a short term quick fix, just get rid of the following code from T4MVC.tt (around line 370):
<#foreach (var method in controller.ActionMethodsUniqueWithoutParameterlessOverload) { #>
[NonAction]
[<#= GeneratedCode #>, DebuggerNonUserCode]
public <#=method.ReturnTypeFullName #> <#=method.Name #>() {
return new T4MVC_<#=method.ReturnType #>(Area, Name, ActionNames.<#=method.ActionName #>);
}
<#} #>
But I'll need to look for a real fix. Normally, this generation happens when the action has no no-param overload. It just needs to detect that an action with all-optional params should basically be treated as a no-param case.
I asked a question earlier today about ActionFilters in ASP.Net MVC. It turned out my problem was really that my ActionFilter is not even running. Among other things I read this article, and I can't find anything he does that I don't.
This is my code:
// The ActionFilter itself
public class TestingIfItWorksAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.TempData["filter"] = "it worked!";
base.OnActionExecuting(filterContext);
}
}
// The Controller Action with the filter applied
[TestingIfItWorks]
public ActionResult Test()
{
var didit = TempData["filter"];
return View();
}
A breakpoint in the filter method is never hit when I debug, and TempData["filter"] holds a null value when the view is rendered.
Why is this not working?
In case it's helpful to anyone using MVC 4/5:
ActionFilters don't run if you get the namespace of your ActionFilterAttribute or IActionFilter wrong: https://stackoverflow.com/a/13710468/188926
Use System.Web.Http.Filters for Web API, System.Web.Mvc for standard MVC actions.
As in the question, the filter attribute will simply be ignored (no error) if you get it wrong, which makes it difficult to diagnose.
Based on your comments to another answer
When testing via unit tests, the filter is not invoked. If you want to invoke the filter then you'll need mimic the ControllerActionInvoker. It's probably better, to test the filter itself in isolation, then use reflection to ensure that the filter is applied to your action with the correct attributes. I prefer this mechanism over testing the filter and action in combination.
Original
Surely you need an override on your method otherwise you aren't actually replacing the method on the base class. I would have expected the compiler to complain that you needed either a new or override on it. If you don't include the override keyword, it will behave as if you used new. Since the framework invokes it as an ActionFilterAttribute, this means that your method will never get called.
Quoting from MSDN:
If the method in the derived class is
not preceded by new or override
keywords, the compiler will issue a
warning and the method will behave as
if the new keyword were present.
In addition to what tvanofosson said, your action method isn't actually rendering anything to the view. Does your view have a <%=TempData["Filter"].ToString()%> statement or something similar?
I have a problem with a sample routing with the preview 5 of asp.net mvc.
In the AccountController I have 2 actions:
public ActionResult Delete()
public ActionResult Delete(string username)
While trying to look for Account/Delete or Account/Delete?username=davide the ControllerActionInvoker throws a exception saying that Delete request is ambiguous between my tow actions methods.
The default route in the global.asax hasn't been changed.
Shouldn't the action invoker understand what's the method to call looking in the parameters list?
Using the preview 4 I hadn't these kind of problem performing the same operation.
Any idea?
Solution found!
With the introduction of the ActionNameAttribute, it's now necessary to filter manually which method to call depending on the request. This is done by the ActionSelectionAttribute.
Full explanation here: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx
I can't say for sure why this is happening. But you might want to consider only having the Delete(string username) action and removing the parameter-less overload.
Because string is nullable my understanding is that simply calling Account/Delete will invoke the action with a null username parameter which you can then test for at the beginning of the action method.
What I'd do is ditch the blank Delete(), and only use Delete(string username)
In your url routing you'd have something similar to "/{Controller}/{Action}/{username}/" ?
If you have "/{Controller}/{Action}/{Id}/" you'd be better off doing Delete(string id) and that way just using the url to handle this "/Account/Delete/davide/"
That said use your default route which should be something like the default Id is ""
Then in your Delete(string id) method have:
public ActionResult Delete(string id)
{
if(string.IsNullOrEmpty(id)) return EmptyID();
// Continue normal Delete method
}
public ActionResult EmptyID()
{
// The method you were going to have on a blank delete.
}
That or just wrap it up in the one method on an if {} else {}
Either way I'd just be going with the one method and doing a default on your username/id in your route of an empty string and handle it that way.
If you want to contact me on further follow up to what I mean, or whatever will help, ping me at andrew# my domain on my info page.
Edit: Ah pretty much what Berko said anyway, I'm not sure how Named Attributes would help - so please post a comment here detailing it for other guys who find the same issues! :)
Its ambiguous because the two controller action are the same post method..
You can only used that in form posting scenario for example you are submitting a form data that uses HTTP post..