I try to use swagger ui for rest api documentation. Rest API created with WCF service. I solved cors issue, service accepts OPTIONS method. But request has a header parameter which name is SessionUUID, this parameter doesn't send to service. When i debug the service side, this parameter does not come. This problem is only in Chrome. Chrome blocks to header params in cors request.
Cors problem's solving on WCF Service like this;
I added new behavior and header params are defined
public class EnableCorsEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior
{
...
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
var requiredHeaders = new Dictionary<string, string>();
requiredHeaders.Add("Access-Control-Allow-Origin", "*");
requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Accept-Language, SessionUUID, Origin, X-Custom-Header, sessionuuid");
requiredHeaders.Add("Access-Control-Allow-Credentials", "true");
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new ServiceInspector(requiredHeaders));
}
...
}
I added response headers in inspector. Header parameter which named as SessionUUID is checked in AfterReceiveRequest but it is null, because of this problem.
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
try
{
WebHeaderCollection headers = WebOperationContext.Current.IncomingRequest.Headers;
string requestTo = request.Headers.To.ToString();
if (!requestTo.Contains("login") && request.GetType().Name != "InternalByteStreamMessage")
{
if (headers["SessionUUID"] == null)
{
throw new RestAppException("401", "Oturum bilgisi bulunamadı.");
}
else
{
DateTime requestDate = DateTime.Now;
string sessionUUID = headers["SessionUUID"] != null ? headers["SessionUUID"].AsString() : headers["sessionuuid"].AsString();
sessionManager.CheckSessionByUUID(sessionUUID, requestDate);
sessionManager.UpdateExpireDate(sessionUUID, requestDate);
sessionManager.SetSessionByUUID(sessionUUID);
}
}
}
catch (RestAppException ex)
{
ex.Message = "İşlem Başarısız";
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return null;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
foreach (var item in requiredHeaders)
{
httpHeader.Headers.Add(item.Key, item.Value);
}
}
How can i solve this chrome issue?
You will have to add a Chrome extension to allow custom headers.
Here are some that you could use:
WebRequestAPI
Mod Header
In case you want to try out Mozilla Firefox, use its extension modify-headers.
I had a similar problem . It may be related to the url.
My problem was solved when I entered with "www".
If already you entered with "www", delete this and try again.
Related
I'm learning Blazor.
I have created a Blazor WASM App with the "ASP.NET Core Hosted" option.
So I have 3 projects in the solution: Client, Server and Shared.
The following code is in the Client project and works perfectly when the endpoint is correct (obviously). But at some point I made a mistake and messed up the request URI, and then I noticed that the API returned an HTML page with code 200 OK (as you can see in the Postman screenshot below the code).
I expected one of my try-catches to get this, but the debugger jumps to the last line (return null) without throwing an exception.
My first question is why?
My second question is how can I catch this?
I know fixing the endpoint fixes everything, but would be nice to have a catch that alerts me when I have mistyped an URI.
Thanks.
private readonly HttpClient _httpClient;
public async Task<List<Collaborator>> GetCollaborators()
{
string requestUri = "api/non-existent-endpoint";
try
{
var response = await _httpClient.GetFromJsonAsync<CollaboratorsResponse>(requestUri);
if (response == null)
{
// It never enters here. Jumps to the last line of code.
}
return response.Collaborators;
}
catch (HttpRequestException)
{
Console.WriteLine("An error occurred.");
}
catch (NotSupportedException)
{
Console.WriteLine("The content type is not supported.");
}
catch (JsonException)
{
Console.WriteLine("Invalid JSON.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
it is a never good idea to use GetFromJsonAsync, You are not the first who are asking about the strange behavior. Try to use GetAsync. at least you will now what is going on.
var response = await client.GetAsync(requestUri);
if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<CollaboratorsResponse>(stringData);
... your code
}
else
{
var statusCode = response.StatusCode.ToString(); // HERE is your error status code, when you have an error
}
I have an application with both MVC and 'new' ApiController endpoints in ASP.NET Core 2.2 co-existing together.
Prior to adding the API endpoints, I have been using a global exception handler registered as middleware using app.UseExceptionHandler((x) => { ... } which would redirect to an error page.
Of course, that does not work for an API response and I would like to return an ObjectResult (negotiated) 500 result with a ProblemDetails formatted result.
The problem is, I'm not sure how to reliably determine in my 'UseExceptionHandler' lambda if I am dealing with an MVC or a API request. I could use some kind of request URL matching (eg. /api/... prefix) but I would like a more robust solution that won't come back to bite me in the future.
Rough psuedo-code version of what I'm trying to implement is:
app.UseExceptionHandler(x =>
{
x.Run(async context =>
{
// extract the exception that was thrown
var ex = context.Features.Get<IExceptionHandlerFeature>()?.Error;
try
{
// generically handle the exception regardless of what our response needs to look like by logging it
// NOTE: ExceptionHandlerMiddleware itself will log the exception
// TODO: need to find a way to see if we have run with negotiation turned on (in which case we are API not MVC!! see below extensions for clues?)
// TODO: ... could just use "/api/" prefix but that seems rubbish
if (true)
{
// return a 500 with object (in RFC 7807 form) negotiated to the right content type (eg. json)
}
else
{
// otherwise, we handle the response as a 500 error page redirect
}
}
catch (Exception exofex)
{
// NOTE: absolutely terrible if we get into here
log.Fatal($"Unhandled exception in global error handler!", exofex);
log.Fatal($"Handling exception: ", ex);
}
});
});
}
Any ideas?
Cheers!
This might be a bit different than what you expect, but you could just check if the request is an AJAX request.
You can use this extension:
public static class HttpRequestExtensions
{
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (request.Headers == null)
return false;
return request.Headers["X-Requested-With"] == "XMLHttpRequest";
}
}
And then middleware with an invoke method that looks like:
public async Task Invoke(HttpContext context)
{
if (context.Request.IsAjaxRequest())
{
try
{
await _next(context);
}
catch (Exception ex)
{
//Handle the exception
await HandleExceptionAsync(context, ex);
}
}
else
{
await _next(context);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
//you can do more complex logic here, but a basic example would be:
var result = JsonConvert.SerializeObject(new { error = "An unexpected error occurred." });
context.Response.ContentType = "application/json";
context.Response.StatusCode = 500;
return context.Response.WriteAsync(result);
}
see this SO answer for a more detailed version.
If you want to check whether the request is routed to ApiController, you could try IExceptionFilter to hanlde the exceptions.
public class CustomExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
if (IsApi(context))
{
HttpStatusCode status = HttpStatusCode.InternalServerError;
var message = context.Result;
//You can enable logging error
context.ExceptionHandled = true;
HttpResponse response = context.HttpContext.Response;
response.StatusCode = (int)status;
response.ContentType = "application/json";
context.Result = new ObjectResult(new { ErrorMsg = message });
}
else
{
}
}
private bool IsApi(ExceptionContext context)
{
var controllerActionDesc = context.ActionDescriptor as ControllerActionDescriptor;
var attribute = controllerActionDesc
.ControllerTypeInfo
.CustomAttributes
.FirstOrDefault(c => c.AttributeType == typeof(ApiControllerAttribute));
return attribute == null ? false : true;
}
}
Thanks to all of the advice from others, but I have realised after some more thought and ideas from here that my approach wasn't right in the first place - and that I should be handling most exceptions locally in the controller and responding from there.
I have basically kept my error handling middleware the same as if it was handling MVC unhandled exceptions. The client will get a 500 with a HTML response, but at that point there isn't much the client can do anyway so no harm.
Thanks for your help!
I need to find the status of the Network interface card in UWP. I did not find any way to get the status. Is it possible in UWP to get the status of NIC?
You are able to get it from NetworkInterface
bool isNetwokAvailable = NetworkInterface.GetIsNetworkAvailable();
or subscribe on event NetworkInformation.NetworkStatusChanged
NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
NetworkInterface Class
NetworkInformation Class
EDIT:
Ping something
bool async IsNetworkAvailable()
{
HttpWebRequest request = WebRequest.CreateHttp(ApiRootUrl);
request.ContentType = "application/json;charset=UTF-8";
try
{
await request.GetResponseAsync();
return true;
}
catch
{
return false;
}
}
Hello I have been using Spring 3 for my project, I have been stuck in on point.
if(ajax){
User user = userTemplate.getUser(form.getCreator_id());
int isPremium = user.getPremium();
if ( isPremium == 1 ){
Map<String,String> resultMap = new HashMap<String,String>();
response.setHeader("Access-Control-Allow-Headers", "*");
response.addHeader("Access-Control-Allow-Origin", "*");
resultMap.put("result", "success");
return new Gson().toJson(resultMap);
}else{
return "redirect:/f/redirectedUrl?url="+form.getWeb_page();
}
}
redirectedUrl controller is just for redirecting, but if the request is ajax request then i want to response the request as json.
How can I achieve this, thanks.
Edit : I can understand if request is ajax or not. My problem is if it is ajax i want to response json, if it is not then i want to redirect.
Use this code in your controller to identify if request is ajax or not and based on that you can add your logic.
boolean ajax = "XMLHttpRequest".equals(
getRequest().getHeader("X-Requested-With"));
You can decide it from header("X-Requested-With") of your httpRequest object.
public ModelAndView getDetails(HttpServletRequest request, HttpServletRespone response) {
if(ajax) {
try {
new MappingJacksonHttpMessageConverter().write(object, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
} catch(Exception e) {
logger.error("Error when converting to json");
}
return null;
} else {
return new ModelAndView("viewName");
}
}
I have a JSON request, but it seems that it is not hitting the controller. Here's the jQuery code:
$("#ddlAdminLogsSelectLog").change(function() {
globalLogSelection = $("#ddlAdminLogsSelectLog").val();
alert(globalLogSelection);
$.getJSON("/Administrative/AdminLogsChangeLogSelection", { NewSelection: globalLogSelection }, function(data) {
if (data.Message == "Success") {
globalCurrentPage = 1;
} else if (data.Message == "Error") {
//Do Something
}
});
});
The alert is there to show me if it actually fired the change event, which it does.
Heres the method in the controller:
public ActionResult AdminLogsChangeLogSelection(String NewSelection)
{
String sMessage = String.Empty;
StringBuilder sbDataReturn = new StringBuilder();
try
{
if (NewSelection.Equals("Application Log"))
{
int i = 0;
}
else if (NewSelection.Equals("Email Log"))
{
int l = 0;
}
}
catch (Exception e)
{
//Do Something
sMessage = "Error";
}
return Json(new { Message = sMessage, DataReturn = sbDataReturn.ToString() }, JsonRequestBehavior.AllowGet);
}
I have a bunch of Json requests in my application, and it seems to only happen in this area. This is a separate area (I have 6 "areas" in the app, 5 of which work fine with JSON requests). This controller is named "AdministrativeController", if that matters.
Does anything jump out anyone as being incorrect or why the request would not pass to the server side?
Look at the GET in Firebug or Fiddler.
Either:
There is no GET, in which case your browser cached the results from last time (cough, IE, cough); change the cache policy on the response.
There is a GET, but it doesn't match your route; fix the routing or the JavaScript, as appropriate.
As it turns out, if the Area name and Controller name are the same, it looks like MVC gets a little confused. Im not sure if this is a bug on my side, or something witH MVC, but when I remove the "/" from the name in the Json request (ie. "Administrative/Action" instead of "/Administrative/Action") it works just fine. A colleague was the one to figure this one out for me, he found some forum response on it and showed me what they did. Once I removed the "/" it worked just fine.