I am trying to make n-tier application where web api is kept on a different class library. I made a TestController controller class in a different class library and my code goes like this
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
namespace PkrUni.SMS.Api
{
[ApiController]
[Produces("application/json")]
[Route("api/[controller]")]
public class TestController : ControllerBase
{
public IEnumerable<string> Get()
{
return new string[] { "Chris", "Hadfield" };
}
}
}
Now my question is how can I access this web api on my main project. I had already added a reference to that class library on main project but it doesn't working. My api is not working. 404 Page Not Found error shows up while trying to access the api.
This is my project structure.
What am I doing wrong please help me.
Try to install Microsoft.AspNetCore.Mvc package in the razor class library.
And in startup.cs use:
services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("PkrUni.SMS.Area.Api")));
Refer to here.
I test your scenario by creating a asp.net core2.2 MVC project and razor class library without any problem. Below is my structure:
Related
I'm trying to deploy my ASP.NET Core MVC Web App with Web API, i.e. I have both MVC and API controllers in the same folder.
It works fine on localhost but on IIS when I create a Virtual Directory, the path gets added to the domain.
I can find it using window.location.pathname
I can append the 'api/Get' and it works like (questions is my virtual directory)
http://example.com/questions/api/Question/GetAll
But when I navigate to other pages then then controller name also gets appended and then it causes issues.
e.g. if I navigate to the 'Question' page (QuestionController), the URL becomes
http://example.com/questions/newquestion/api/Question/Create
instead of
http://example.com/questions/api/Question/Create
How can I fix it?
Here is my Asp.Net core api.
[ApiController]
public class ScheduleController : ControllerBase
{
[HttpGet]
public List<PathologistSchedule> GetPathologistScheduleByDate(DateTime taskDate)
{
return pathologistRepository.GetPathologistScheduleByDate(taskDate).ToList();
}
}
I call this api from PathologistScheduleController's view using jquery.
Here's the error I get:
GET http://localhost:51434/PathologistSchedule/api/Schedule/?sort=&group=&filter=&taskDate=2020-11-13T21%3A16%3A47.507Z 404 (Not Found)
TIA.
A
If you have API and MVC projects in one solution you have to config your solution to run multiple projects.
You can use route attribute like this for each of your APIs
[Route("~/api/Question/GetAll")]
will give you Url http://example.com/api/Question/GetAll.
Or
[Route("~/api/Question/Create")]
will give Url http://example.com/api/Question/Create.
And it will not depend on the controller name or folder.
UPDATE because of the question update:
Use this code please:
public class ScheduleController : ControllerBase
{
[Route("~/api/Schedule/GetPathologistScheduleByDate/{taskDate?}")]
public List<PathologistSchedule> GetPathologistScheduleByDate(DateTime taskDate)
{
return pathologistRepository.GetPathologistScheduleByDate(taskDate).ToList();
}
}
for testing try this route in your browser:
http://localhost:51434/api/Schedule/GetPathologistScheduleByDate/2020-11-13T21%3A16%3A47.507Z
But basically for APIs you don't need to use any controller or action name. You can use any names you like, for example:
[Route("~/api/Pathologist/GetSchedule/{taskDate?}")]
or
[Route("~/api/GetPathologistSchedule/{taskDate?}")]
or even
[Route("~/api/{taskDate?}")]
The route just should be unique.
I added a variable in the 'appsettings.json' and 'appsettings.Development.json' called baseURL and had 'appsettings.json' set to '/VirtualDirectoryName/' and kept the one in 'appsettings.Development.json' as '/'.
Appended this variable when calling APIs.
I am trying to learn Web API and MVC. I, initially created a basic MVC project. Now, in the controllers folder ,I added a WebAPI controller.
In the WebAPI controller, I added the below code
public class SampleController : ApiController
{
[HttpPost]
public IHttpActionResult SampleData()
{
var userID = User.Identity.GetUserId();
return Ok();
}
}
The Method User.Identity.GetUserId() works fine in MVC.
I searched about on SO and found the following thread
User.Identity.GetUserId() method not working in a Web Api 2 Controller
This was not that helpful for me, as in my case I have added the API controller as part of the MVC project itself in the controllers folder.I have not created a separate project for WebAPI.
The above mentioned thread talks about the accesstoken already being present in code , whereas in my case, I dont see that code anywhere, as I just added only a single web api controller.
I am using POSTMAN for calling the API.
I have also looked at the following link
https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/individual-accounts-in-web-api
In this case too, a separate WebAPI project is being talked about and not a single controller.
If I am mistaken somewhere, kindly guide me on the same.
I am a beginner in DocuSign API Implementation. I have a webhook type action in my controller, thate inherited from ApiController base class.
public class DocuSignController : ApiController
But it's always shows a message like below.
The controller for path '/XXX/XXX' was not found or
does not implement IController.
I know this issue the ApiController not inherited from the IController class. But the webhook needed ApiController (I hope i am right).
My website have already SSL enabled (this is required), already published this code into server.
Is any additional configuration needed for this?
The proper class for an MVC DocuSign WebHook receiver in C# is Controller.
namespace DocuSignWebSample.Controllers
{
public class HomeController : Controller
{
Sample Code at https://github.com/jeremylindsayni/Magellanic.DigitalSignature
Overview Blog article at https://jeremylindsayni.wordpress.com/2017/03/11/integrate-docusign-with-a-c-mvc-website/
Reminder, when using other's code, understand your company's licensing related code policy's.
I've just created an empty web api project with .net core 2.0.
I have a default controller and I want now create an integration test.
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
The goal is self host in the integration then enter url api/values and check the return.
NB: I only use wep api 2 and owin, and it was quite easy to do this. But the following link: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/owin says that .net core application should not use owin, so what should we use and how to it?
Take a look at the official documentation for integration testing with ASP.NET Core 2.0: https://learn.microsoft.com/en-us/aspnet/core/testing/integration-testing
The main idea is to create a TestServer instance with a WebHostBuilder configured with your Startup class. You can then get a HttpClient instance from the TestServer to call your self-hosted api
Hi I am new to asp net programming and I am trying to test my asp net application using HttpSelfHostServer
link: http://www.c-sharpcorner.com/UploadFile/2b481f/self-hosting-in-Asp-Net-web-api/
by first setting up the server and then calling GET requests on my server on different controllers. (as described in the link)
This works perfectly when the controllers extend ApiController (which seems to be the base for asp net webapi applications) but it doesnt work for any other controller.
I am trying to get this to work with controllers that return views such as :
public class HomeController
: Controller
{
public ViewResult Index()
{
return View();
}
}
IS this not possible to do with the HttpSelfHostServer? Is there a workaround to this?