0

I've seen many other questions that are similar to my title but none seem to be same issue or supply insight that I need.

I've added a Web API Controller to an existing, legacy 4.5 WebForms application and am trying to get it working with minimal changes to existing code base. So I don't have the typical static WebApiConfig class and other default created items when you create project from scratch. My existing code base is:

Global.asax

protected void Application_Start( Object sender, EventArgs e ) 
{ 
    GlobalConfiguration.Configure( config =>
    {
        config.MapHttpAttributeRoutes();

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

Controller

namespace Modeler.Endpoints
{
    public class KatAppEndpoints : ApiController
    {
        [HttpGet]
        [Route( "api/katapp/calculations" )]
        public IEnumerable<string> GetCalculations()
        {
            return new string[] { "value1", "value2" };
        }
    }
}

Javascript

$(document).ready(function() {
    $.getJSON("api/katapp/calculations")
        .done(function () { debugger; })
        .fail(function (_jqXHR, errorStatus, errorMessage) { debugger; })
        .always(function () { debugger; });
});

When called, I hit the fail and always and the result is:

errorStatus: "error"
errorMessage: "Not Found"

Anything obvious I'm missing?

Update

If instead I change my Controller to...

namespace Modeler.Endpoints
{
    public class KatAppsController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
}

And my javascript to:

$.getJSON("api/katapps")
    .done(function () { debugger; })
    .fail(function (_jqXHR, errorStatus, errorMessage) { debugger; })
    .always(function () { debugger; });

Things work correctly. However, I have a handful of endpoints and wanted to use Attributes, but maybe I'll give up on that if I can figure out how to get another portion in the url (i.e. the /calculations part from above).

5
  • Are you using HTTP or HTTPS? If server want HTTPS and you are sending HTTP that could cause this issue. Most server these days want HTTPS. With 4.5 some still used HTTP. Commented Jun 10, 2021 at 16:51
  • Local dev, so yes, HTTP while developing/testing. Is there a way to allow HTTP? Commented Jun 10, 2021 at 18:38
  • If you debug, do you even hit the GetCalculations() function? Commented Jun 10, 2021 at 19:02
  • No, I never get into GetCalculations(). See my update to the question about some more info. Commented Jun 10, 2021 at 19:08
  • The server has settings like require https. See : learn.microsoft.com/en-us/aspnet/web-api/overview/security/… Commented Jun 11, 2021 at 7:39

1 Answer 1

0

Not sure why, but this comment pointed me in the right spot: https://stackoverflow.com/a/38130308/166231

So even though I want to use attribute routing and might not be close to what controller name is, I still have to end the class name with *Controller. After renaming KatAppEndpoints to KatAppEndpointsController, everything worked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.