Found one example on the whole Internet of the following, and it was on StackOverflow, but there was no follow up whether it had been solved or not. This error happened after I implemented accept header versioning with attribute routing in a WebAPI 2 project.
Message: "An error has occurred." ExceptionMessage: "The given key was
not present in the dictionary." ExceptionType:
"System.Collections.Generic.KeyNotFoundException"
found here on stackoverflow:
How to get controller name when Web API versioning with routing attribues
I am not sure how you implemented this as you have not shared any code, but you can take a look at the following sample demonstrating Web API's attribute routing and versioning through route constraints.
http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/RoutingConstraintsSample/ReadMe.txt
The above sample looks for a custom header called api-version, but you can easily modify this to suit your scenario.
Related
i wish to implement overriding of 404 not found in case of unsupported requests , and respond with a more helpful message to consumer.
I found this Link to ensure this in .net framework however the config override of this way is not supported in .Net core.
var constraintResolver = new DefaultInlineConstraintResolver();
constraintResolver.ConstraintMap.Add("rangeWithStatus",
typeof(RangeWithStatusRouteConstraint));
config.MapHttpAttributeRoutes(constraintResolver);
Above code is content of WebApiConfig file which is no longer needed in asp.net core. What alternative should I do to use ?
For Web Api its normally cleaner and easier to use the middleware feature. You can basically wrap all of your requests in some code which will allow you to handle exceptions. An example of which can be found here:
https://blogs.msdn.microsoft.com/brandonh/2017/07/31/using-middleware-to-trap-exceptions-in-asp-net-core/
I use a similar thing to handle formatting the responses into a uniform structure as well as handling exceptions and outputting different text depending on the specific error
I have web application developed using Asp.Net MVC 5.
I am integrated Elmah.MVC error logging library for error logging and reporting.
I got weird error messages
like the following:
System.Web.HttpException: The controller for path '/manager/html' was not found or does not implement IController.
The source of request was the following ip 221.194.44.229
which is in China.
Another message:
System.Web.HttpException: The controller for path '/888/pakistani-hackers-forcing-pilots-listen-pakistan-kashmir' was not found or does not implement IController.
The source of request was the following ip 39.45.204.204
which is in Pakistan.
I do not know if elmah.mvc is contains malware or such trojan or myserver was hacked how to investigate that issue and where is problems come from.
Do not know if that the correct place to ask such question or not
Mainly there is some one trying to request apath that is not on your site.
The idia is to make you read the massage in the requested path ( like the secound error from your post) that is som kind of spam.
There is nothing to worry about? This happened every day.
But i think you need to enable custom error pages if you are not already do.
I have a web api controller like below.
In swagger output I am having the below image
And when I want to consume it in my another console app, it is showing the below error
Note I have followed the tutorial Customize Swashbuckle-generated API definitions but still getting the same error. Any idea?
I found this while looking for the same question. For me it was a dumb mistake: the controller's methods had the same name. Apparently, Swashbuckle uses the method's name as the Operation Id.
I must need to add the Route Attribute on top of the Action methods. Like below:
So, the API changed into below Swagger UI endpoints
And when I consumed this Swagger in my console app, the below OperationId I got
And I can now call like below (just an example)
So I added an ASMX web service to my MVC4 but when I tried to access it I got a "The Resource could not be found" error. After searching I found the answer here.
In short, I had to add the following IgnoreRoute to my RouteConfig file.
routes.IgnoreRoute("{*x}", new { x = #".*\.asmx(/.*)?" });
I understand the MapRoute function in MVC fairly well, however the IgnoreRoute, not so much. I understand that it's targeting the .asmx postfix but I'm not sure on the how's and why's of this quick fix.
How does this IgnoreRoute work, and exactly why does it make my MVC app magically understand how to find and execute my web service? BTW, My only mapped route, currently, is the default, but is there another/better way of solving this issue using MapRoute or another fix?
ignore route indicates that routing should ignore these requests and ASP.NET processing of these requests will occur.
http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx/
What specifically in WebAPI responds to:
1. http://server/vroot/odata
2. http://server/vroot/odata?$metadata
3. http://server/vroot/odata/Foo
When #3 is requested, I understand that my 'FooController' responds as
configured in my WebApiConfig.cs.
But it is not clear to me how WebAPI responds to #1 or #2. How does it know
what to return? How is that response configured in my code?
UPDATE: Here is a HUGE clue
From http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx
One important thing to realize here is that the controller name, the
action names, and the parameter names all matter. OData controller and
action selection work a little differently than they do in Web API.
Instead of being based on route parameters, OData controller and
action selection is based on the OData meaning of the request URI. So
for example if you made a request for
http://my.server.com/vroot/odata/$metadata, the request would actually get
dispatched to a separate special controller that returns the metadata
document for the OData service. Notice how the controller name also
matches the entity set name we defined previously. I’ll try to go into
more depth about OData routing in a future blog post.
]
returns you the Service Document
returns you the Service Metadata Document
WebAPI knows this because you add a route similar to config.Routes.MapODataRoute("ODataRoute", "odata", model);
Check out this detailed explanation: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/creating-an-odata-endpoint