Home > Back-end >  ASP.NET can not find API controller
ASP.NET can not find API controller

Time:09-26

I'm currently working on a website that needs to fetch a string from an API controller, so that it can display it on the page as an image. Everything works fine when I run the site in Visual Studio, but when I publish it to the website itself, the server always responds with the following:

<Error>
<Message>No HTTP resource was found that matches the request URI 'https://{website_name}/api/Files/GetProfilePicture?userId=cc6afd64-885e-4d8b-a239-42b161d665cc'.</Message>
<MessageDetail>No type was found that matches the controller named 'Files'.</MessageDetail>
</Error>

Other Api controllers in the website are working perfectly fine, but it's specifically this controller that is being stubborn.

Here is the WebApiConfig.cs:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

Here is the view:

@model {website_name}.Models.ProfilePictureGetModel

@{
    if (Model.userId == null)
    {
        Model.userId = Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(User.Identity);
    }

    string style = "border-radius: "   Model.size   "px; height: "   Model.size   "px; width: "   Model.size   "px; object-fit: cover; cursor: pointer";
}

<body>
    <input id="userId" type="hidden" value="@Model.userId">
    <img class="pflPic @Model.userId" style="@style" onclick="location.href = '/Profile?id='   '@Model.userId'" />
    @Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
        var userId=$('#userId').val();
        $.ajax({
            url: "/api/Files/GetProfilePicture/"   userId,
            type: "GET",
            cache: false,
            async: true,
            success: function (result) {
                if (result) {
                    var pics = document.getElementsByClassName("@Model.userId")
                    for (var i = 0; i < pics.length; i  ) {
                        if (pics[i].src == "")
                            pics[i].src = result;
                    }
                }
            }
        });
</script>
</body>

Here is the controller:

namespace {website_name}.Controllers.Api
{
    public class FilesController : ApiController
    {
        ApplicationDbContext _context;

        [HttpGet]
        [WebMethod]
        [ResponseType(typeof(String))]
        public IHttpActionResult GetProfilePicture(string userId)
        {
            var User = _context.Users.FirstOrDefault(x => x.Id == userId);

            if (User == null)
                return NotFound();

            string img = Convert.ToBase64String(User.ProfilePicture);
            if (User != null)
                return Ok("data:image;base64,"   img);
            else
                return NotFound();
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
            base.Dispose(disposing);
        }

        public FilesController()
        {
            _context = new ApplicationDbContext();
        }
    }
}

CodePudding user response:

you can try classical MVC style url, and use full url if your webapi is not inside of your web application

var userId=$('#userId').val();

    $.ajax({
          url: "https://{website_name}/api/Files/GetProfilePicture/" userId,
            type: "GET",
            success: function (result) {
          ......

and add this somewhere inside of the view form of your web application

<input id="userId" type="hidden" value="@Model.UserId">

it should send request to this url

https://{website_name}/api/Files/GetProfilePicture/cc6afd64-885e-4d8b-a239-42b161d665cc

action

[Route("~/api/Files/GetProfilePicture/{userId?}")]
public IHttpActionResult GetProfilePicture(string userId)

CodePudding user response:

There is a problem with how your formatted the request url in your ajax call

   url: "/api/Files/GetProfilePicture/"   userId,

and it should be

   url: "/api/Files/GetProfilePicture?"   userId,
  • Related