I have 2 Get methods on my Web Api 2 controller:
// GET: api/ClientApi
public HttpResponseMessage GetAll()
{
IEnumerable<Client> clients = _clientRepository.GetAll();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, clients);
return response;
}
// GET: api/ClientApi/userId
public HttpResponseMessage GetAllClientsForUser(string userId)
{
IEnumerable<Client> clients = _clientRepository.GetAll();
var clientUsers = _clientUserRepository.FindAll(cu => cu.UserId == userId).ToList();
var clientsForUser = clients.Where(i => clientUsers.Select(cu => cu.ClientId).ToList().Contains(i.Id)).ToList();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, clientsForUser);
return response;
}
// GET: api/ClientApi/clientId
public HttpResponseMessage GetClientById(int id)
{
var client = _clientRepository.GetById<Client>(id);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, client);
return response;
}
Althought the names are different, I get the error:
Not supported by Swagger 2.0: Multiple operations with path
'api/Client' and method 'GET'.
Is there a way to go around this? I tried using an OperationFilter, found this on StackOverflow somewhere but that doesn't work...
Route.config:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I hope this is gonna help you, I fixed my issue this way, go to your swagger config file, add the following at the top:
using System.Linq;
and then around line 175, or use search and find ResolveConflictingActions you should find this line of code:
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
but it is commented out, comment that line of code in, and hopefully that will solve your issue, play around with it, since you might not just want the first one.
above the line of code you will see a short description for it. Hope this was helpful.
Related
I am trying to build a service client to simplify calling my microservices in .net core.
Here is a service client sample:
public ProductServiceClient(SystemEnvironment.MachineEnvironment? environment = null)
{
this.url = ServiceEnvironment.Urls.GetUrl(ServiceEnvironment.Service.Product, environment);
}
private RestClient GetClient(string method)
{
return new RestClient(url + "/api/" + method);
}
private RestRequest GetRestRequest(Method method)
{
var restRequest = new RestRequest(method);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader("Content-Type", "application/json");
return restRequest;
}
public FindProductsResponse FindProducts(FindProductsRequest request)
{
var restRequest = GetRestRequest(Method.GET);
restRequest.AddJsonBody(request);
var client = this.GetClient("Products");
var restResponse = client.Get(restRequest);
return new JsonDeserializer().Deserialize<FindProductsResponse>(restResponse);
}
public void Dispose()
{
}
And here is how I am trying to read it in my .net core api:
[HttpGet]
public ActionResult<FindProductsResponse> Get()
{
var request = "";
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
request = reader.ReadToEnd();
}
var buildRequest = JsonConvert.DeserializeObject<FindProductsRequest>(request);
var products = _service.FindProducts(buildRequest);
if (products != null && products.Any())
{
return new FindProductsResponse()
{
Products = products
};
}
return BadRequest("Not found");
}
However the request variable is always empty after Request.Body has been processed by the StreamReader.
If I make the same request from Postman (also using GET), I get the body just fine.
What am I doing wrong here?
EDIT: This is the unit test calling the api:
[Test]
public void Test1()
{
using (var productServiceClient = new ProductServiceClient())
{
var products = productServiceClient.FindProducts(new FindProductsRequest()
{
Id = 50
}).Products;
}
}
It can be your Request.Body has been already consumed.
Try to call Request.EnableRewind() before to open the StreamReader.
I'm not sure why you are manually doing it. It looks like you are reinventing the wheel. ASP.NET Core already does that for you.
This is what your service should look like:
[HttpGet] // oops, GET requests will not allow Bodies, this won't work
public ActionResult<FindProductsResponse> Get([FromBody]FindProductsRequest buildRequest)
{
// skip all the serialization stuff, the framework does that for you
var products = _service.FindProducts(buildRequest);
if (products != null && products.Any())
{
return new FindProductsResponse()
{
Products = products
};
}
return BadRequest("Not found");
}
And if you don't want to redo all the busy work that is retyping all the code on the client side, I suggest you read up on swagger (probably in the form of Swashbuckle). Client code can be generated. Even from within Visual Studio, if you right-click on the project and in the context menu pick "Add REST API Client...". Please don't erroneously hand-code what can be generated flawlessly by a machine instead. I don't really know what went wrong in your specific case, but searching bugs that could be avoided altogether is just busywork, that time should be spent on other parts of the program.
I just realized this is a GET request. ASP.NET will not recognize bodies for GET-Requests. You will need to make it a PUT or POST request or put your parameters in the query string.
If you happen to make that mistake as often as I did, you might want to write some unit tests that cover this. Because .NET is not helping you there. Been there, done that..
I have searched many times for my problem but I couldn't find any solution.
the problem which faced is I wrote method yesterday in asp.net core API which is return file response as streaming video when I tried to test on my local machine it's working very well, but when uploading it to my VPS server I got the 500 internal server error so please what is the problem.
the following my code which I used:
[HttpPost("youtube")]
public async Task<IActionResult> GetYoutubeVideoURL([FromBody] string url)
{
if (string.IsNullOrEmpty(url))
{
return Ok(new { Error = "Please provide your url for fetch video." });
}
var id = YoutubeClient.ParseVideoId(url);
if (id == null)
{
return Ok(new { Error = "Video id is not available, try again later." });
}
var client = new YoutubeClient(); //This should be initialized in YoutubeController constructor.
var mediaInfoSet = await client.GetVideoMediaStreamInfosAsync(id);
var mediaStreamInfo = mediaInfoSet.Audio.WithHighestBitrate();
var mimeType = $"audio/{mediaStreamInfo.Container.GetFileExtension()}";
var fileName = $"{id}.{mediaStreamInfo.Container.GetFileExtension()}";
return File(await client.GetMediaStreamAsync(mediaStreamInfo), mimeType, fileName, true);
}
I'm currently updating an old site to use the Hot Towel framework and one of the the things the current site does is load an image using the HTTPMessageResponse. This works fine in the the old set up however on the new set up the image is not displayed and going to the URL produces...
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers: { Content-Type: image/png }
I've had a look at the response both apps produce, and the responses from the following function match (the response is the same as above and the contents are a perfect match)...
namespace OPC.Controllers
{
public class ImageController : Controller
{
public HttpResponseMessage GetImage(int imageId)
{
var blob = new byte[10];
var context = new OPCEntity();
var ImageString = context.sp_GetImageData(imageId);
foreach (var Result in ImageString)
{
blob = Result.Data;
}
Image image;
var ms = new MemoryStream();
ms.Write(blob, 0, blob.Length);
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(ms.ToArray());
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
return result;
}
}
}
The image is set in the JS using...
image.setAttribute('src', document.location.href.match(/(^[^#]*)/)[0] + '/Image/GetImage?imageId=' + imageDetails.imageId);
Does anyone have any ideas on what I could be missing, or if anything in the Hot Towel framework could be interfering.
Many Thanks in advance.
Well I have missed something important... the fact that it should be...
public class ImageController : ApiController{}
However I have also found that the breeze web API config does conflict with the normal web API config so for the time being I've had to change BreezeWebApiConfig.cs to the following
public static class BreezeWebApiConfig {
public static void RegisterBreezePreStart() {
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "BreezeApi",
routeTemplate: "api/{controller}/{action}"
);
}
}
When calling the method shown below, Request is always null. I have some simple methods returning JSON data from controllers in an MVC4 app with controllers using ApiController as a base class. The code for my directory function is as follows:
public HttpResponseMessage GetDirectory() {
try {
var dir = r.GetDirectory();
if (dir == null) {
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}
var response = Request.CreateResponse(HttpStatusCode.OK, dir, "application/json");
response.Headers.Location = new Uri(Request.RequestUri, "directory");
return response;
} catch (Exception ex) {
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
When this method is called, 'dir' is loaded properly from r.GetDirectory(). But Request is null. So naturally Request.CreateResponse() fails, and so on. I am looking for reasons why Request would be null, or for a rewrite that allows the return to remain an HttpResponseMessage.
This is being called (in my unit test project) with:
var ctrl = new DirectoryController();
var httpDir = ctrl.GetDirectory();
Thanks for your help.
Olav Nybø left me the hint that led to the answer. In response to ASP.NET WebApi unit testing with Request.CreateResponse, jonnii suggested the following code:
controller.Request = new HttpRequestMessage();
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
This led to a nice way to test controllers:
var controller = new RecordsController();
controller.Request = new HttpRequestMessage();
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
var json = controller.GetAllRecords(id);
var records = JsonConvert.DeserializeObject<DynamicRecordSet>(json.Content.ReadAsStringAsync().Result);
I am using TweetSharp in a Windows Phone project and no matter what I do, I can't post a tweet with media.
I am getting the exception 195: Missing or invalid parameter.
I read that usually this can be a cause of invalid data, like the stream that I provide is invalid.
I have tried other way but nothing works , I get the same exception ...
The sharing code, simplified is like this:
MediaLibrary library = new MediaLibrary();
var picture = library.Pictures[0];
var options = new SendTweetWithMediaOptions
{
Images = new Dictionary<string, Stream> { { picture.Name, picture.GetImage() } },
Status = TweetTextBox.Text,
};
AutentificateTwitterService().SendTweetWithMedia(options, (status, response) =>
_dispatcher.BeginInvoke(() =>
{
DonePosting();
if (response.StatusCode == HttpStatusCode.OK)
{
_lastPostId = status.Id;
}
else
{
MessageBox.Show(String.Format(
"There was an error sending image to Twitter{0}{1}",
Environment.NewLine,
response.Error));
}
}));
I tried sharing with linqtotwitter and worked but TweetSharp is more appropriate for my project.
Finally after some time I've found the problem to this and I am sure to other more WP and SendTweetWithMediaOptions related problems.
The thing is that if you dig into SendTweetWithMedia the way it is now you will get to TwitterService.cs where WithHammock will be called, is just the images are not passed as parrameters, so they get lost right there :)
I did fix this passing the parameters and adding
private void WithHammock<T>(WebMethod method, Action<T, TwitterResponse> action, string path, IDictionary<string, Stream> files, params object[] segments) where T : class
{
var url = ResolveUrlSegments(path, segments.ToList());
var request = PrepareHammockQuery(url);
request.Method = method;
request.QueryHandling = QueryHandling.AppendToParameters;
foreach (var file in files)
{
request.AddFile("media[]", file.Key, file.Value);
}
WithHammockImpl(request, action);
}
I will try and see if I can Pull this so that everyone else can have this fix.
Hope this helps.