0

Im trying to create simple form, which will pass variables to C# method, which then will add whem to database.

public static bool Add(string username, string password, string email, string ip)
    {
        User newUser = new User { id = HomeController.db.Users.Count() + 1, createdOn = DateTime.Now, email = email, ipAddress = ip, isDeleted = false, password = password, username = username, info = new AdditionalInfo(), notices = new List<Notice>() };
        newUser.address.user = newUser;
        if (!HomeController.db.Users.ToArray().Contains(newUser, new EqualByData()))
        {
            HomeController.db.Users.Add(newUser);
            HomeController.db.SaveChanges();
        }
        else return false;
        return true;
    }

This method is in C:\NTPortal\NTportal\NTportal\Controllers

When I tried to call it in angularjs Controller:

if (!$scope.email_bad && !$scope.pass_bad && !$scope.pass_not_match) {
        console.log('no errors')
        var Add = function (username, password, email, ip) {
            var config = {
                params: {
                    username: username,
                    password: password,
                    email: email,
                    ip: ip
                }
            }
            $http.get('../../Controllers/UserController/Add', config)
            .success(function (data) {
                if (data) {
                    alert('Vartotojas priregistruotas');
                }
            })
            .error(function (error) {
                alert('Ivyko klaida');
            })
        }
        Add($scope.username, $scope.password, $scope.email, ipAddress);
    }

Angular is in C:\NTPortal\NTportal\NTportal\Views\Home

An error I get, is GET http://localhost:2016/Controllers/UserController.cs/Add?email=f@&password=bandymas1&username=f 404 (Not Found)

Any help would be appreciated

7
  • Remove the '.cs' from the end of the controller name in your get url: $http.get('../../Controllers/UserController/Add', config) Commented Apr 10, 2017 at 20:15
  • Tried it, same result Commented Apr 10, 2017 at 20:17
  • You may also want to look into resolving your url (e.g. Have your view create a JavaScript variable to hold the resolved path. var path = @Url.Action("Add", "User"); Commented Apr 10, 2017 at 20:18
  • You can't call a controller directly from JS. You have to go through a web server. Commented Apr 10, 2017 at 20:22
  • Is it possible that the C# API function does not have the HttpGet attribute? Commented Apr 10, 2017 at 20:24

2 Answers 2

1

Try this, you have to put the url to your action:

 $http.get('http://localhost:12345/User/Add', config)

Instead of http://localhost:12345, put your url.

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

2 Comments

GET http://localhost:2016/User/Add?email=f@&password=bandymas1&username=f 404 (Not Found)
Try to remove the static from your action.
0

I think you should add [HttpGet] or [HttpPost] Method Attribute above Add function like:

[HttpGet]
public static bool Add(..)
{
 //...
}

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.