如何定义路线?需要任何其他操作重定向到单个操作。 [英] how to define a route? need any other action redirects to single action.

查看:55
本文介绍了如何定义路线?需要任何其他操作重定向到单个操作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我正在使用MVC4,我是新手。我需要确定从同一个控制器采取任何操作的一条路由重定向到单个/同一个动作。我没有找到足够的信息。





例如。



我有一个家庭控制器,它包含几个动作。其中一个是内容。我需要的是所谓的重定向到内容操作的任何操作,该网址与旧操作相同。

解决方案

您好Amolpatil,



您可以通过两种方式实现,



1)使用重定向行动

 [HttpPost] 
public ActionResult 索引(字符串名称)
{
ViewBag.Message = Dot Net Tricks< /跨度>;
//与Response.Redirect()一样 Asp.Net WebForm
return RedirectToAction( MyIndex);
}

public ActionResult MyIndex()
{
ViewBag.Msg = ViewBag.Message; //分配值:
返回 查看 MyIndex);
}



2)在App_Start文件夹中使用RouteConfig.cs

为此尝试这个链接 [ ^ ]



我希望这对你有所帮助



问候,

RK


我通过使用这种路线映射解决了这个问题。







 routes.MapRoute(
name: CMS
url: {controller} / {category} / {id}
默认值: new
{
category = UrlParameter.Optional,
page = 36
action = 内容
},
约束: new
{
controller = 主页
}
);







我将类别设为可选,控制器为约束,因此它与我合作


HI,

I am using MVC4, and I am new to it. I need to determine one route that takes any action from a same controller redirects to a single/same action. I didnt found sufficient information.


eg.

I have a Home controller and it contains several actions. one of them is "Content". What I need is like any action called redirect to Content action only, the url remains with the same as older action.

解决方案

Hi Amolpatil,

You can do it in two ways,

1) Using redirect to action

[HttpPost]
public ActionResult Index(string Name)
{
 ViewBag.Message = "Hi, Dot Net Tricks";
 //Like Response.Redirect() in Asp.Net WebForm
 return RedirectToAction("MyIndex");
}

public ActionResult MyIndex()
{
 ViewBag.Msg = ViewBag.Message; // Assigned value : Null
 return View("MyIndex");
}


2) Using RouteConfig.cs in App_Start folder
For this try this link[^]

I hope this helps you a bit

Regards,
RK


I solved this by using this mapping of route.



routes.MapRoute(
          name: "CMS",
          url: "{controller}/{category}/{id}",
          defaults: new
          {
              category = UrlParameter.Optional,
              page = 36,
              action = "Content"
          },
          constraints: new
          {
              controller = "Home"
          }
     );




I put the category optional and controller as constraint, so it worked with me.


这篇关于如何定义路线?需要任何其他操作重定向到单个操作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆