c# - 如何通过普通路由重定向

标签 c# asp.net-mvc routes

我有行动

public ActionResult Edit(int id)

如果 id 为空,我需要重定向到操作索引。如何在路由中做到这一点?

我试过:

routes.MapRoute(
                name: "Redirect from empty controller/edit to controller/list",
                url: "{controller}/Edit",
                defaults: new { controller = "Home", action = "Index" }
            );

但这并没有帮助。

最佳答案

嗯,不确定路由,但如果有帮助,您可以使用 return RedirectToAction("index", "home");(其中 index 是您的操作方法,home 是您想要的 Controller 访问它)。

关于c# - 如何通过普通路由重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485678/

相关文章:

c# - 使用 Linq 从列表中获取所有匹配值的索引

c# - 什么是 C# 中的 Delphi "shl"?

javascript - 从同一页面调用两个按钮不起作用

c# - 如何使用 ASP.NET MVC 返回动态 CSS?

reactjs - Routes.push() 在 NextJS 上没有按预期工作

c# - asp.net MVC 5 中的 Web api 路由失败

c# - 通过.net使用java web服务-数据不一致

c# - 如何以编程方式实例化或模拟窗口?

c# - 在 MVC 中清除 View 引擎会破坏站点地图面包屑样式

node.js - React-Router如何防止重定向