【发布时间】:2018-03-05 06:09:17
【问题描述】:
我正在将 Wcf 服务用于 Angular JS 应用程序。我使用类库项目创建了 wcf 服务。我将它托管到 Service.svc 文件中。单击提交按钮时,我在文本框中输入了所需的信息,它显示以下错误。
System.NullReferanceException: Object reference not set an instance of an object.
Angular JS application showing following error ..
POST http://localhost:52098/HalifaxIISService.svc/RegisterUser 400 (Bad Request)
这是界面。
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/RegisterUser")]
bool RegisterUser(UserLogin UserLogin);
这是接口的实现..
public bool RegisterUser(UserLogin UserLogin)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
// SqlConnection is in System.Data.SqlClient namespace
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spRegisterUser", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter username = new SqlParameter("@Username", UserLogin.Username);
// FormsAuthentication calss is in System.Web.Security namespace
string encryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(UserLogin.Password, "SHA1");
SqlParameter password = new SqlParameter("@Password", encryptedPassword);
SqlParameter email = new SqlParameter("@Email", UserLogin.Email);
cmd.Parameters.Add(username);
cmd.Parameters.Add(password);
cmd.Parameters.Add(email);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
if (ReturnCode == -1)
{
return false;
}
else
{
return true;
}
}
}
这里是 Script.Js 文件代码..
/// <reference path="../angular.min.js" />
var app = angular.module("WebClientModule", [])
.controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) {
$scope.OperType = 1;
//1 Mean New Entry
//To Clear all input controls.
function ClearModels() {
$scope.OperType = 1;
$scope.Username = "";
$scope.Password = "";
$scopr.Email = "";
}
$scope.createuser = function () {
var User = {
Username: $scope.Username,
Password: $scope.Password,
Email: $scope.Email
};
if ($scope.OperType === 1) {
var promisePost = myService.post(User);
promisePost.then(function (pl) {
$scope.User_Id = pl.data.User_Id;
window.location.href = "/Login/Index";
ClearModels();
}, function (err) {
$scope.msg = "Password Incorrect !";
console.log("Some error Occured" + err);
});
}
}
}]);
app.service("myService", function ($http) {
//Create new record
this.post = function (User) {
var request = $http({
method: "post",
url: "http://localhost:52098/HalifaxIISService.svc/RegisterUser",
data: User
});
return request;
}
})
我不知道为什么会收到此错误。任何帮助将不胜感激。
【问题讨论】:
标签: javascript c# angularjs wcf ado.net