简体   繁体   English

在POST数据中从Api控制器重定向到MVC控制器

[英]Redirection from Api Controller To MVC Controller in POST data

I'm new to MVC and Web API. 我是MVC和Web API的新手。 I got struck here. 我被打到这里了。 We have an APIController and MVC Controller. 我们有一个APIController和MVC控制器。 Here API controller has CreateEmployee(Employee Data), inside the Action method, It should call to MVC controller Register(Employee Emp) Action method. 这里的API控制器在Action方法中具有CreateEmployee(Employee Data),它应该调用MVC控制器的Register(Employee Emp)Action方法。

  • How to Redirect from APIController To MVC Controller..? 如何从APIController重定向到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. 当我尝试在WebApi的CreateEMployee中创建MVC控制器的对象时,数据显示在MVC控制器中,但未插入ASPNetUsers表中。 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 // MVC控制器

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

A Web Api action should not redirect to an MVC action. Web Api操作不应重定向到MVC操作。 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. Web Api的全部目的是促进与瘦客户机的交互,这可能完全无法呈现甚至解析HTML文档。

More likely than not, you want to do this because you do not want to repeat the registration code in your MVC action. 您极有可能想要执行此操作,因为您不想在MVC操作中重复注册代码。 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. 但是,正确的方法是将代码分解到类库中,以使您的Web Api操作和MVC操作都可以利用。

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. 基本上,它构建了MVC的url帮助器,因此您可以将结果字符串重定向到。

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. 我尚未对此性能进行测试,因此,请再次不要只是从WebAPI重定向到MVC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM