NaveenGNK
NaveenGNK

Reputation: 93

Redirection from Api Controller To MVC Controller in POST data

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.

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

Upvotes: 0

Views: 2748

Answers (2)

Matt Edwards
Matt Edwards

Reputation: 133

I agree with Chris that you should not do this, and there are better ways to reuse code.

That said, I have a function I've used in the past to help build links for other reasons (like for sending emails) that would work for you.

return Redirect(MvcURL("Index", "Home", null));

Basically, it builds MVC's url helper so you can get the resulting string to redirect to.

private static string MvcURL(string routeName, string controller, object routeValues)
{
    var urlHelper = new System.Web.Mvc.UrlHelper(
        new RequestContext(
            new HttpContextWrapper(HttpContext.Current),
            HttpContext.Current.Request.RequestContext.RouteData), 
        RouteTable.Routes);

    return urlHelper.Action(routeName, controller, routeValues, HttpContext.Current.Request.Url.Scheme);
}

I have not tested this for performance, so again, please don't just redirect from WebAPI to MVC.

Upvotes: 1

Chris Pratt
Chris Pratt

Reputation: 239440

A Web Api action should not redirect to an MVC action. The whole point of a Web Api is to facilitate interaction with thin clients, which may be totally incapable of rendering or even parsing an HTML document.

More likely than not, you want to do this because you do not want to repeat the registration code in your MVC action. However, the correct approach, then, is to factor out that code into a class library that both your Web Api action and MVC action can both utilize.

Upvotes: 1

Related Questions