【问题标题】:Redirection from Api Controller To MVC Controller in POST data在 POST 数据中从 Api 控制器重定向到 MVC 控制器
【发布时间】:2016-10-08 19:54:20
【问题描述】:

我是 MVC 和 Web API 的新手。我在这里受到打击。我们有一个 APIController 和 MVC 控制器。这里API控制器有CreateEmployee(Employee Data),在Action方法里面,它应该调用MVC控制器Register(Employee Emp)Action方法。

  • 如何从 APIController 重定向到 MVC Controller..?

  • 这怎么可能..?

当我尝试在 WebApi 的 CreateEMployee 中创建 MVC 控制器的对象时,数据显示在 MVC 控制器中,但未插入到 ASPNetUsers 表中。如何在不创建对象的情况下做到这一点。建议将不胜感激..

APIController

[HttpPost]

Public Void CreateEmployee(Employee Emp)

{

  //how to redirect here to MVC Controller without creating object of the  mvc contoller..

}

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

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api


    【解决方案1】:

    Web Api 操作不应重定向到 MVC 操作。 Web Api 的全部意义在于促进与瘦客户端的交互,瘦客户端可能完全无法呈现甚至解析 HTML 文档。

    您很可能想要这样做,因为您不想在 MVC 操作中重复注册代码。但是,正确的方法是将该代码分解到一个类库中,您的 Web Api 操作和 MVC 操作都可以使用。

    【讨论】:

      【解决方案2】:

      我同意 Chris 的观点,您不应该这样做,并且有更好的方法来重用代码。

      也就是说,我过去曾使用过一个功能来帮助建立适合您的其他原因(例如发送电子邮件)的链接。

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

      基本上,它构建了 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);
      }
      

      我还没有测试过它的性能,所以再次强调,请不要只是从 WebAPI 重定向到 MVC。

      【讨论】:

        猜你喜欢
        • 2017-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        相关资源
        最近更新 更多