Display custom error message in wcf invalid method in route table - wcf

I have implemented a RESTful API using wcf and I use System.Web.Routing.RouteTable.Routes.Add(...) to map the methods to URLs. If a user types an invalid url (i.e. a url that does not map to a method), say myapiurl/geeet/ instead of myapiurl/get/ then currently a standard error message like "Method not allowed" is displayed.
How do I customize this error message?
I have tried Application_Error in Global.asax, but it does not catch the exception.
I have also implemented a IErrorHandler, IServiceBehavior, but it also does not catch this error.
Neither does the WebHttpBehavior I have implemented.

The "Method not allowed" (HTTP status code 405) message means the server is receiving a request from a client using an HTTP method (GET, POST, PUT, etc.) that is not allowed by the server (IIS). For example the client is submitting a PUT request and IIS is not configured to accept PUT. I think by default, IIS only supports GET and POST.
A bad URI, as you describe in your question, generally results in a HTTP status code of 404 resource not found. Sound like your IIS configuration may be causing your issue since you can't trap the exception at either the ASP.NET or WCF layer.

Related

OSB - Is there a way to generate default errors for internal errors?

I have a validate action in the OSB request pipeline which raises an error if the request is not according the schema.
In the error handler, I remove the header info and add some required header info before replying with a failure. However the response I get is the request body and not the fault which is in $fault. Is there a way to have OSB generate a standard soapFault using the $fault variable instead of having to manually create the error response body?
I'm having the same issue with the general error handler defined for the business service and the pipeline error handler when internal errors are raised.
Additionally, Is there a best-practice on how this is usually done for normal services and in case of passthrough services?
thanks

How to get details exception from published Web Api

Is there any way to get detail exception & Stack trace from deployed Web Api?
Even if I deployed in Debug mode, do I get detail error traces?
I got bellow message from server API
"message": "Processing of the HTTP request resulted in an exception.
Please see the HTTP response returned by the 'Response' property of
this exception for details."
This happened only in server, same code running perfectly in local machine.
Did you check the content-type for the request that you are performing?
Sometimes this error comes because you don't specify this value, e.g. "application/json" in the headers of the request.
If your scenario is to have a central place to log any exceptions that happen when requests are being processed, then I would suggest to take a look at 5.1 version of Web API (released couple of days back), specifically the Global Error Handling feature.
http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-21#global-error
If you go to the above page,you should notice ExceptionLoggerContext which gives you details of the exception.
About Web API versions 5.0 and before:
In these versions there was no really a central place for catching exceptions. ExceptionFilterAttributes caught exceptions for only certain areas of Web API.

Web API - How to Prevent 404 HTML Response?

How can I change ASP.NET Web API to never return a text/html 404 response? I'd rather it send back XML/JSON with an ExceptionMessage or Message. It doesn't make sense to return HTML from an API, IMO.
Just to clarify, this is for cases when the URL truly is invalid.
Another problem is that I am hosting MVC and Web API in the same project, so I need to respond differently. I am guessing it will depend on whether the URL starts with "api".
You do not get those HTML data if you simply call
throw new HttpResponseException(HttpStatusCode.NotFound);
somewhere in your ApiController's methods.
You get this page only when routing mechanism can't fit your request to any of your ApiController's methods. So the HTML message is attached on the application level not in ApiControllers. Here is very good example how you can handle errors in your ASP.NET application Custom error pages on ASP>NET MVC3.
I would even go further and check if the request comes to WebAPI (not to MVC), and if so redirect to IHttpActionResult in ErrorsContoller.
How to prepare IHttpActionResult for response you can read here Action Results in Web API 2
Have you tried using an Exception Filter? This may allow you to capture exceptions and then set the response type "applications/json", message, etc.

WCF Server Error. The content type text/html of the response message does not match the content type of the binding

I've got a WCF Service, which calls a webservice, running on my development IIS server (IIS 7). I've added it as a service reference to a C# Website Project and it adds fine.
However, when I try to call any of the service contracts, I get the following error:
The content type text/html
of the response message does not match
the content type of the binding
(text/xml; charset=utf-8). If using a
custom encoder, be sure that the
IsContentTypeSupported method is
implemented properly. The first 1024
bytes of the response were:
'Blocked Web
Page
thanks in advance
BB
The error message says it clearly: you're getting back an HTML page instead of your service response. Looking at the fragment of that page listed in the error message, you're probably not authroized to use that service.
Try to connect to the service URL in a browser - you should probably see a page explaining that you're not allowed to access the page. Most likely, this is a permissions issue.
You need to configure WCF Tracing and find out what's happening on the server side.

The remote server returned an error : 400 badrequest in WCF

In our project, we are calling the .svc file directly from asp.net web page and I receive the error "The remoter server returned an error:(400) bad request.
Our project architecture is, we are using .svc file in our web application and the .cs file for the svc in writter in another class library project. From aspx, we are calling the WCF service directly without adding reference or anything. I cannot change the concept, because it is our standard. I'm able to add service reference and call those methods, but I wanted to call the method directly from .svc url.
I'm pissed off for 2 days and could not resolve the error. We are using HttpWebRequest to get the response from the service. Basically, the service will take Data Transfer Object(DTO) as input and returns the same(DTO) as output with only one value.
Check the following code:
HttpStatusCode statusCode = HttpHelper.PostXmlRequestValue(requestXMLInput,
string.Format("http://{0}/{1}/MySample.svc/webhttp/MyMethodName",
Request.ServerVariable["HTTP_HOST"], Request.ApplicationPath);
The same code works in one machine but not in the other. I have checked the configuration and everything is same, but still I receive the same error.
When I use the .svc url in my machine, it works, but gives a message "Method not allowed". When I checked the same url in the working machine, I got the same message.. I believe there is some simple thing I'm missing out. I couldn't find, as I'm new to WCF.
When using HttpWebRequest the "Method not allowed" error is for example that you are sending a Http GET, when the service expects a Http POST.
HttpWebRequest is a REST based configuration, this limits the complexity of the DTO's that you can send.