Is there something like "RewriteToAction"?

Knowing RedirectToAction , I was looking for something similar in order to maintain a stable URL for the user and still be responsible from one action to another.

Since I found Google 's null results on this topic, I could completely try to solve the XY problem .

However, I will try to explain why I think it might be necessary.

Scenario:

 public ActionResult A(int id) { var uniqueID = CalculateUniqueFromId(id); // This compiles. return RedirectToAction("B", new { uniqueID }); // This does not compile. return RewriteToAction("B", new { uniqueID }); } public ActionResult B(Guid uniqueID) { var model = DoCreateModelForUnique(uniqueID); return View("B", model); } 

In the above code, action A calculates a guiding tool from an integer and passes it to another action.

Workaround:

I could change the above code to something like this:

 public ActionResult A(int id) { var uniqueID = CalculateUniqueFromId(id); var model = DoCreateModelForUnique(uniqueID); return View("B", model); } public ActionResult B(Guid uniqueID) { var model = DoCreateModelForUnique(uniqueID); return View("B", model); } 

This will work as expected.

However, in more complex scenarios, I would like to use "server-side redirection" (aka rewrite) from time to time.

Alternative workaround:

I could also use HttpContext.RewritePath , for example. inside Global.asax Application_BeginRequest do rewrite.

This seems to me โ€œout of context with MVC,โ€ although it works as expected.

My question is:

Is there an elegant, MVC-integrated way to do something like RewriteToAction ?

Update 1:

Luke commented on a promising link:

How to simulate Server.Transfer in ASP.NET MVC?

I will try to find out if this suits my needs.

+5
source share
1 answer

What about

 public ActionResult A(int id) { var uniqueID = CalculateUniqueFromId(id); return B(uniqueID); } public ActionResult B(Guid uniqueID) { var model = DoCreateModelForUnique(uniqueID); return View("B", model); } 

?

+2
source

Source: https://habr.com/ru/post/1266840/


All Articles