Pregunta

The scenario in I am having MVC application in a project and web api project in separate project.Both projects are in same solutions. My web api project having UserManagerAPIController. It has CreateUser Method. I want to access CreateUser from UserManagerAPIController in My MVC controller. I want to pass JSON data to create user. I am trying in my Index.aspx in mvc project to call this :

function SendCustomer() {
                var parameters = { "FirstName": "John", "LastName": "Doe", "Username": "johndoe","Password" :"john@123"}

                $.ajax({
                    contentType: 'application/json',    
                    url: "UserManager/CreateUser",                       
                    type: "POST",
                    data: JSON.stringify(parameters),
                    success: function (data) {

                    },
                    error: function (xhr) {
                        alert(xhr.toString());
                    }
                });
            }

But unable to hit CreateUser method in another project. What should be the issue ?

¿Fue útil?

Solución

There are many suggestions,

  1. Why you have made two project? WebApi can easily manage in MVC application.
  2. To call web api you have to use /api/UserManager url.
  3. It also require to follow form method. if you have mentioned [HttpGet] annotation on CreateUser, you must use Get form method while calling this api.
  4. If you do not want to integrate webapi in MVC application it will throw an error of cross origin resource sharing. for that you will have to defined CreateUser as [EnableCors] annotation.

For reference check this - http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top