TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ninad Shinde
NA
13
4.6k
Multiple post Method in single API Controller
Feb 7 2019 11:29 PM
Dear all,
I am Begineer in MVC 5 and Angular 6 in which I am developing one web application.
There is one angular component(web page) - User.html and User.ts where I am trying to save data in two tables separately i.e.
and
.
On angular component(web page) i.e. User.html and User.ts, I have separate button to save User header data in
table and there is button which opens popup window to save User details data in
table.
I am passing data from Angular component to APIcontroller in MVC 5(UserController.cs).
On UserController.cs, I have two POST methods i.e. one for Header and another for Details
UserController.cs
using
DomainDb.Models;
using
DomainDb.Repository;
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Http;
using
System.Web.Http.Cors;
using
System.Web.Mvc;
namespace
CURDApis.Controllers
{
/// This is controller class which will be used to expose endpoints.
[EnableCors(origins:
"*"
, headers:
"*"
, methods:
"*"
)]
public
class
UserController : ApiController
{
/// User repository which will be used to write actual business logic.
private
IUserRepository _userRepository;
public
UserController()
{
// we can inject repository from unity container.
_userRepository = UnityFactory.ResolveObject
();
}
//Getting Users Rights for CORS
public
List
GetRightsList(
string
srights,
string
srights1)
{
return
_userRepository.GetRightsList(srights, srights1);
}
// GET All User
public
List
Get()
{
return
_userRepository.Get();
}
//Get particular User on the basis of id
public
User Get(
int
id)
{
return
_userRepository.Get(id);
}
//Deactivates User on the basis of id
public
void
Delete(
int
id)
{
_userRepository.Delete(id);
}
//This method will insert or update User component data in
table
public
void
Post([FromBody]User value)
{
_userRepository.Post(value);
}
//This method will insert or update User component data on popup window to
table
public
void
post([FromBody]User svalue,
string
suser)
{
_userRepository.Post(svalue, suser);
}
}
}
Code of public void Post([FromBody]User value) written in a REPOSITORY called as UserRepository.cs - code for inserting or updating data in table
///
///Inserting or Updating User header data
///
object of user.
///
public
void
Post(User objUser)
{
GetAuthentications objGetAuthenication =
new
GetAuthentications();
objGetAuthenication.Add = objUser.Add;
objGetAuthenication.Modify = objUser.Modify;
objGetAuthenication.Deactivate = objUser.Deactivate;
objGetAuthenication.View = objUser.View;
string
auth = objGetAuthenication.GetAuthentication(objGetAuthenication);
string
InsertQuery = objUser.UserID == 0 ?
@"INSERT IGNORE INTO mstuserheader(UserID,UserName, UserPassword,AuthenticationType,UserRightsID,Activate)
VALUES (?UserID,?UserName, ?UserPassword, ?AuthenticationType,?UserRightsID,?Activate );
SELECT LAST_INSERT_ID();
" :
@"
UPDATE mstuserheader SET UserName = ?UserName,UserPassword = ?UserPassword,AuthenticationType = ?AuthenticationType,
UserRightsID = ?UserRightsID,Activate = ?Activate WHERE UserID = " + objUser.UserID;
using
(MySqlConnection connEpion =
new
MySqlConnection(ConnStr))
{
connEpion.Open();
int
ID = objUser.UserID;
using
(MySqlCommand objcmd =
new
MySqlCommand(InsertQuery))
{
objcmd.Connection = connEpion;
objcmd.Parameters.AddWithValue(
"UserID"
, objUser.UserID);
objcmd.Parameters.AddWithValue(
"AuthenticationType"
, auth);
objcmd.Parameters.AddWithValue(
"UserRightsID"
, objUser.UserRightsID);
objcmd.Parameters.AddWithValue(
"Activate"
, objUser.Activate);
int
i = objcmd.ExecuteNonQuery();
}
connEpion.Close();
}
}
Code of public void post([FromBody]User svalue, string suser) written in a REPOSITORY called as UserRepository.cs - code for inserting or updating data in table
public
void
Post(User objUserRights,
string
suser)
{
GetAuthentications objGetAuth =
new
GetAuthentications();
objGetAuth.Add = objUserRights.Add;
objGetAuth.Modify = objUserRights.Modify;
objGetAuth.Deactivate = objUserRights.Deactivate;
objGetAuth.View = objUserRights.View;
string
auth = objGetAuth.GetAuthentication(objGetAuth);
string
query =
string
.Empty;
using
(MySqlConnection connEpion =
new
MySqlConnection(ConnStr))
{
connEpion.Open();
// checking whether userRightsID and ComponentID is exist or not
using
(MySqlCommand objcmd =
new
MySqlCommand(
"select count(*) from mstuserrightsdetails where UserRightsID = "
+ objUserRights.UserRightsID +
" and ComponentID = "
+ objUserRights.ComponentID))
{
objcmd.Connection = connEpion;
//converting result into integer which come as result using ExecuteScalar()
int
i = Convert.ToInt32(objcmd.ExecuteScalar());
if
(i == 0)
{
// Performing Insert here when it is not exist
query = @"INSERT IGNORE INTO mstuserrightsdetails(UserRightsID,ComponentID,AuthenticationType,Activate)
VALUES (?UserRightsID,?ComponentID, ?AuthenticationType,?Activate );";
}
else
{
//Performing Update here when it is exist
query = @"UPDATE mstuserrightsdetails SET AuthenticationType = ?AuthenticationType,
Activate = ?Activate WHERE UserRightsID =
" + objUserRights.UserRightsID + "
and ComponentID = " + objUserRights.ComponentID;
}
objcmd.CommandText = query;
objcmd.Parameters.AddWithValue(
"UserRightsID"
, objUserRights.UserRightsID);
objcmd.Parameters.AddWithValue(
"ComponentID"
, objUserRights.ComponentID);
objUserRights.AuthenticationType = auth;
objcmd.Parameters.AddWithValue(
"AuthenticationType"
, objUserRights.AuthenticationType);
objcmd.Parameters.AddWithValue(
"Activate"
, objUserRights.Activate);
i = objcmd.ExecuteNonQuery();
}
connEpion.Close();
}
}
When I am trying to call second post method(public void Post(User objUserRights,string suser)) for saving UserDetails data then I am getting default values for User class and it gets stored into table. here is code for User.cs
here is code for User.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DomainDb.Models
{
//Class of User contains local vaiables for database fields
public
class
User
{
public
int
UserID {
get
;
set
; }
public
int
UserDetailsID {
get
;
set
; }
public
int
ComponentID {
get
;
set
; }
public
string
UserName {
get
;
set
; }
public
string
UserPassword {
get
;
set
; }
public
bool
Add {
get
;
set
; }
public
bool
Modify {
get
;
set
; }
public
bool
Deactivate {
get
;
set
; }
}
}
Reply
Answers (
1
)
Is ASP.NET CORE 2.0 Worth Learning?
How to Generate Token without using migration in WEBAPI