I'm new to MVC and Web API. I got struck here. We have an APIController and MVC Controller. Here API controller has CreateEmployee(Employee Data), inside the Action method, It should call to MVC controller Register(Employee Emp) Action method.
How to Redirect from APIController To MVC Controller..?
How Is It Possible..?
When I tried by creating Object Of the MVC controller in CreateEMployee of WebApi, the data is showing in MVC Controller, but not Inserting into ASPNetUsers Table. How to do it without creating object. Suggestions would be appreciated..
APIController
[HttpPost]
Public Void CreateEmployee(Employee Emp)
{
//how to redirect here to MVC Controller without creating object of the mvc contoller..
}
//MVC Controller
public class EmployeeRegController : Controller
{
ApplicationDbContext db = new ApplicationDbContext();
private ApplicationUserManager _userManager;
public EmployeeRegController()
{
}
public EmployeeRegController(ApplicationUserManager userManager)
{
UserManager = userManager;
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public ActionResult Register(Employee Emp)
{
try
{
RegisterViewModel RVM = new RegisterViewModel();
RVM.Email = Emp.EmpFirstName + "." + Emp.EmpLastName +"@gmail.com";
RVM.Password = "Password@123";
RVM.ConfirmPassword = "Password@123";
db.Employees.Add(Emp);
db.SaveChanges();
CreateEmp(RVM);
}
catch(Exception ex1)
{
throw ex1;
}
}
public void CreateEmp(RegisterViewModel regModel)
{
if (ModelState.IsValid)
{
try
{
var user = new ApplicationUser() { UserName =regModel.Email, Email = regModel.Email };
var result = UserManager.Create(user, regModel.Password);
}
catch (Exception ex1)
{
throw ex1;
}
}
}
// GET: EmployeeReg
public ActionResult Index()
{
return View();
}
}