C# POST数据中从Api控制器重定向到MVC控制器
我不熟悉MVC和Web API。我在这里被击中了。我们有一个APIController和MVC控制器。这里API控制器有CreateEmployee(Employee Data),在Action方法中,它应该调用MVC控制器Register(Employee Emp)Action方法C# POST数据中从Api控制器重定向到MVC控制器,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我不熟悉MVC和Web API。我在这里被击中了。我们有一个APIController和MVC控制器。这里API控制器有CreateEmployee(Employee Data),在Action方法中,它应该调用MVC控制器Register(Employee Emp)Action方法 如何从APIController重定向到MVC控制器 怎么可能 当我尝试在WebApi的CreateEMployee中创建MVC控制器的对象时,数据显示在MVC控制器中,但没有插入到ASPNetUsers表中。
- 如何从APIController重定向到MVC控制器
- 怎么可能
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();
}
}
公共类EmployeeRegController:控制器
{
ApplicationDbContext db=新的ApplicationDbContext();
私有应用程序用户管理器\u用户管理器;
公共雇员控制者()
{
}
公共EmployeeRegController(ApplicationUserManager用户管理器)
{
UserManager=UserManager;
}
公共应用程序管理员用户管理器
{
得到
{
返回_userManager??Request.GetOwinContext().GetUserManager();
}
专用设备
{
_userManager=value;
}
}
公共行动结果登记册(员工环境管理计划)
{
尝试
{
RegisterViewModel RVM=新的RegisterViewModel();
RVM.Email=Emp.EmpFirstName+“+Emp.EmpLastName+”@gmail.com”;
RVM.Password=”Password@123";
RVM.ConfirmPassword=”Password@123";
db.Employees.Add(环境管理计划);
db.SaveChanges();
createmp(RVM);
}
捕获(异常ex1)
{
抛出ex1;
}
}
public void CreateEmp(RegisterViewModel regModel)
{
if(ModelState.IsValid)
{
尝试
{
var user=new ApplicationUser(){UserName=regModel.Email,Email=regModel.Email};
var result=UserManager.Create(user,regModel.Password);
}
捕获(异常ex1)
{
抛出ex1;
}
}
}
//得到:雇员
公共行动结果索引()
{
返回视图();
}
}
Web Api操作不应重定向到MVC操作。Web Api的全部目的是促进与瘦客户机的交互,瘦客户机可能完全无法呈现甚至解析HTML文档
更可能的情况是,您希望这样做是因为您不希望在MVC操作中重复注册代码。但是,正确的方法是将代码分解到Web Api操作和MVC操作都可以使用的类库中。Web Api操作不应重定向到MVC操作。Web Api的全部目的是促进与瘦客户机的交互,瘦客户机可能完全无法呈现甚至解析HTML文档
更可能的情况是,您希望这样做是因为您不希望在MVC操作中重复注册代码。然而,正确的方法是将代码分解到一个类库中,您的Web Api操作和MVC操作都可以利用它。我同意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。我同意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