Today I’ve noticed a post in Sitecore SDN forum regarding RedirectToAction() is not working in Sitecore MVC. In this blog post I am going to explain how we can redirect to regular MVC controller action in Sitecore MVC project. We cannot use RedirectToAction() in Sitecore MVC as it interrupts the page rendering process in Sitecore. See below diagram made by David Morrison to get the overview of Sitecore MVC Request Pipeline Execution Lifecycle.
For the demo purpose; I’ve created a controller rendering GetBook which will get the book id using data source and will display specific book details. If data source is set to null then I’ve to redirect to Error Page (or Redirect to anywhere as per your requirement).
Below are the few ways to perform redirection (returning/rendering a view) in Sitecore MVC:
For the demo purpose; I’ve created a controller rendering GetBook which will get the book id using data source and will display specific book details. If data source is set to null then I’ve to redirect to Error Page (or Redirect to anywhere as per your requirement).
Below are the few ways to perform redirection (returning/rendering a view) in Sitecore MVC:
- Redirect to specified URL (Sitecore Page / ASPX Page / External URL) using Redirect(): Redirect() tells MVC to redirect to specified URL instead of rendering HTML. In this case, browser receives the redirect notification and makes a new request for the specified URL. URL in the browser's address bar gets updated. This acts similar to Response.Redirect() in Asp.Net WebForm. Moreover, Redirect() also cause the browser to receive a 302 redirect response status code within your application.
public ActionResult DisplayBookDetail() { string DataSourceId = RenderingContext.Current.Rendering.DataSource; if (DataSourceId != null) { // Write your logic here return View(); } else { return Redirect("~/CustomASPXPages/Error.aspx"); } }
Use RedirectPermanent() instead of Redirect() if you want to return 301 redirect response status code. - Redirect to another View: return View("ViewName") tells MVC to generate HTML to be displayed for the specified view and sends it to the browser. This acts similar to Server.Transfer() in Asp.Net WebForm. return View() doesn't make a new requests, it just renders the view without changing URLs in the browser's address bar.
public ActionResult DisplayBookDetail() { string DataSourceId = RenderingContext.Current.Rendering.DataSource; if (DataSourceId != null) { // Write your logic here return View(); } else { return View("Error"); } }
- Redirect to non-Sitecore Page using RedirectToRoute(): In RouteConfig.cs file you need to add a route that triggers MVC action. The routes for the MVC Web Application are defined in the RouteConfig.cs under the App_Start folder of the MVC Project.
You can modify RouteConfig.cs and use the RegisterRoutes function to register the custom route. The route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The RouteConfig.cs contains the following code:
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); RouteTable.Routes.MapRoute("ErrorDetails", "GetBook/Error", new { controller = "GetBook", action = "Error" }); } }
The RouteConfig.cs contains a static RegisterRoutes function which is called from the Global.asax file.protected void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes); }
RedirectToRoute redirects to a specific route defined in the Route table.public ActionResult DisplayBookDetail() { string DataSourceId = RenderingContext.Current.Rendering.DataSource; if (DataSourceId != null) { // Write your logic here return View(); } else { return RedirectToRoute("ErrorDetails"); } }
- Redirect to Sitecore Page using RedirectToRoute():Use below code to redirect to Sitecore Page using RedirectToRoute()
public ActionResult DisplayBookDetail() { string DataSourceId = RenderingContext.Current.Rendering.DataSource; if (DataSourceId != null) { // Write your logic here return View(); } else { //Get Sitecore Item where you want to redirect Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse("{24240FF2-B4AA-4EB2-B0A4-63E027934C38}")); var pathInfo = LinkManager.GetItemUrl(item, UrlOptions.DefaultOptions); return RedirectToRoute(MvcSettings.SitecoreRouteName, new { pathInfo = pathInfo.TrimStart(new char[] { '/' }) }); } }
Van
ReplyDeleteizmir
Artvin
Tunceli
Eskişehir
WCVH
https://titandijital.com.tr/
ReplyDeleteçanakkale parça eşya taşıma
kırıkkale parça eşya taşıma
erzurum parça eşya taşıma
burdur parça eşya taşıma
87M3HA
E9EA9
ReplyDeleteÇankırı Evden Eve Nakliyat
Bilecik Şehir İçi Nakliyat
Ünye Petek Temizleme
Muğla Şehir İçi Nakliyat
Balıkesir Evden Eve Nakliyat
Kilis Şehir İçi Nakliyat
Paribu Güvenilir mi
Ünye Çekici
Trabzon Şehirler Arası Nakliyat
A5CB8
ReplyDeleteÇankaya Fayans Ustası
Tekirdağ Boya Ustası
Çorum Evden Eve Nakliyat
Antep Evden Eve Nakliyat
Eryaman Boya Ustası
Trabzon Parça Eşya Taşıma
Giresun Lojistik
Ardahan Lojistik
Şırnak Şehir İçi Nakliyat
EABA6
ReplyDeletebinance referans
DE55F
ReplyDeleteledger live
shiba
onekey
poocoin
metamask
arbitrum
avax
galagames
poocoin
8E3C7
ReplyDeletemexc
kripto para nasıl alınır
paribu
gate io
bybit
vindax
gate io
mobil 4g proxy
bitmex
0762F
ReplyDeletemexc
kraken
kucoin
copy trade nedir
gate io
toptan sabun
probit
kripto ne demek
binance referans kod
53843
ReplyDeletehuobi
paribu
telegram kripto
kraken
kredi kartı ile kripto para alma
bitcoin hesabı nasıl açılır
telegram en iyi kripto grupları
binance
kripto ne demek
BCE4B
ReplyDeletecointiger
kripto telegram
bitcoin ne zaman çıktı
poloniex
bitcoin ne zaman yükselir
en iyi kripto grupları telegram
bybit
bitexen
okex
9777C
ReplyDeleteücretli görüntülü şov
F83BA
ReplyDeletewhatsapp ücretli şov
3A8DF
ReplyDeletegörüntülü güvenilir şov
8DDA6
ReplyDeletewhatsapp sanal show
90302
ReplyDeletegörüntülü show
731CABAF85
ReplyDeletesanal seks
sanal sex
cam show
görüntülü show
sohbet hattı
seks hattı
görüntülü seks
sex hattı
görüntülü sex
Thank you for being youu
ReplyDelete5550C02E44
ReplyDeletecam şov
görüntülü show
canli web cam show
görüntülü şov whatsapp numarası
telegram görüntülü şov
görüntülü şov
telegram show
skype show
ücretli show
C86C9818DE
ReplyDeletebufalo çikolata
geciktirici
bayan azdırıcı damla
degra
ereksiyon hapı
lifta
viga
delay
vigrande
18AD7C555A
ReplyDeletetürk beğeni satın al