2017-04-10 37 views
0

我試圖創建簡單的表單,它將變量傳遞給C#方法,然後將數據庫添加到數據庫。在angularjs中調用c#函數

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; 
    } 

這種方法在C:\NTPortal\NTportal\NTportal\Controllers

當我試圖調用它angularjs控制器:

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); 
    } 

角是C:\NTPortal\NTportal\NTportal\Views\Home

錯誤我得到的,是GET http://localhost:2016/Controllers/UserController.cs/[email protected]&password=bandymas1&username=f 404 (Not Found)

任何幫助將理解

+0

在獲取URL從控制器名稱末尾的」的.cs':'$ HTTP .get('../../ Controllers/UserController/Add',config)' –

+0

試過了,結果相同 –

+0

你可能也想看看解決你的url(例如,讓你的視圖創建一個JavaScript變量來保存解析路徑。var path = @ Url.Action(「Add」,「User」);' –

回答

1

試試這個,你必須把網址給你的行動:

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

相反的http://localhost:12345,把你的網址。

+0

'GET http:// localhost:2016/User/Add?email = f @&password = bandymas1&username = f 404(Not Found)' –

+0

嘗試從您的操作中移除靜態。 –

0

我想你應該添加[HTTPGET]或[HttpPost]方法屬性上面加功能,如:

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