Asp.net web API in .net core

Daniel Main asked on March 10, 2023 22:07

I am in the process of converting the V12 Portal version to a .net Core MVC and am a bit confused.

The solution is using a modified version of the Medio Clinic project from the online classes as a base

We have an existing Asp.net web API that we very sucessfully used in the V12 portal app, which I am attempting to bring over. I have looked at the documentations here: https://docs.xperience.io/integrating-3rd-party-systems/using-asp-net-web-api-with-xperience, https://docs.xperience.io/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code, https://docs.xperience.io/custom-development/applying-customizations-in-the-xperience-environment as well as looking at the code in the V12 solution.

The problem is that I cannot figure out where the module should go and how the site code will understand it is there. I am fairly new to the .net Core architecture so it seems a bit like magic for some items.

But I think the issue is in the documentation for creating the module it states: 1: Open your Xperience project in Visual Studio (using the WebApp.sln file). 2: Create a class in the module's code folder.

There curreently is not a code folder. In V12 there was one called app_code but there is no such folder in V13, Or am I being stupid and just need to create it, or is that now the function of the Old_app_code folder?

Recent Answers


Liam Goldfinch answered on March 11, 2023 02:41

It sounds like you're using MedioClinic as an example for your migrated Kentico Xperience 13 website? You just need to add ApiControllers to the project like this.

This is a REST WebAPI controller which is included in the demo website. You can do the same thing in your project and migrate the controllers from the Kentico 12 Portal Engine website.

0 votesVote for this answer Mark as a Correct answer

Daniel Main answered on March 16, 2023 20:12 (last edited on March 16, 2023 20:13)

Well, I think I adapted it fine, but I am still not able to access it. Is there somewhere I need to register it?

Here is the base code:

namespace QuitLogix.Controllers
{
    [ApiController]
    //[Route("api/[controller]")]
    [Route("api/ProsAndCons")]
    public class ProsAndConsController : ControllerBase
    {
        private readonly IPageBuilderDataContextRetriever _pageBuilderDataContextRetriever;
        private readonly IPageRetriever _pageRetriever;
        private readonly ISiteService _siteService;
        private readonly IFileService _fileService;
        private readonly IOptionsMonitor<XperienceOptions> _optionsMonitor;
        private readonly ProsConsDataRepository _prosConsDataRepository;


        public enum ItemType
        {
            Pro,
            Con
        }

        public class ProsAndConsModel
        {
            public List<ProsAndCons> Pros { get; set; }
            public List<ProsAndCons> Cons { get; set; }
            public HttpStatusCode StatusCode { get; set; }
        }



        public ProsAndConsController(
                 IPageBuilderDataContextRetriever pageBuilderDataContextRetriever,
                 IPageRetriever pageRetriever,
                 ISiteService siteService,
                 IFileService fileService,
                 IOptionsMonitor<XperienceOptions> optionsMonitor,
                 ProsConsDataRepository prosConsDataRepository)
        {
            _pageBuilderDataContextRetriever = pageBuilderDataContextRetriever ?? throw new ArgumentNullException(nameof(pageBuilderDataContextRetriever));
            _pageRetriever = pageRetriever ?? throw new ArgumentNullException(nameof(pageRetriever));
            _siteService = siteService ?? throw new ArgumentNullException(nameof(siteService));
            _fileService = fileService ?? throw new ArgumentNullException(nameof(fileService));
            _optionsMonitor = optionsMonitor ?? throw new ArgumentNullException(nameof(optionsMonitor));
            _prosConsDataRepository = prosConsDataRepository ?? throw new ArgumentNullException(nameof(prosConsDataRepository));
        }

        [HttpGet]
        [ValidateAntiForgeryToken]
        public async Task<ProsAndConsModel> Get(string data)
        {
            var result = new ProsAndConsModel()
            {
                Pros = new List<ProsAndCons>(),
                Cons = new List<ProsAndCons>()
            };
            try
            {...

I am just not sure where I am going wrong with this.

0 votesVote for this answer Mark as a Correct answer

Daniel Main answered on March 23, 2023 22:24 (last edited on March 23, 2023 22:27)

Well after a bit of fiddling I went smaller and am just trying a test route.

using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace APIControllers
{
    [ApiController]
    public class ClassController : ControllerBase
    {

        public class testModel
        {
            public int id { get; set; }
            public string data { get; set; }
        }


        public ClassController() { }

        [HttpGet("api/class/{id}")]
        [ValidateAntiForgeryToken]
        public ActionResult<testModel> Get(int id = 0)
        {
            var model = new testModel();
            model.data = "test data";
            model.id = id;

            return Ok(model);
        }
    }
}

with this when I call it I get the response:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"Bad Request","status":400,"traceId":"00-5ae3d3fac567846c7931ddc8fb5b08e6-af8527bc78a0818b-00"}

Any ideas?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.