I think that you won't be able to post back directly to block's controller. That's pretty tricky. Is it possible for you to have a AccountController for instance with Logout method?
If you are dealing with multiple languages in your site, remember to include language routing value (in this case - code fragment posts back to the same page):
Html.BeginForm(null, null, new { language = ContentLanguage.PreferredCulture.Name }, FormMethod.Post)
You can not go directly to a EPiServer block but you can go directly to a MVC-controller.
For exampel create a Controller like this:
public class LogoutController : Controller
{
[HttpPost]
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Clear();
UrlHelper url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
Redirect(UrlHelpers.PageLinkUrl(url, PageReference.StartPage).ToHtmlString());
}
}
After that add a route to this like this:
routes.MapRoute("Logout",
"Logout/{action}",
new { controller = "Logout", action = "Logout" });
And change your beginform to this:
@using (Html.BeginForm("Logout", "Logout", new { language = ContentLanguage.PreferredCulture.Name },FormMethod.Post))
{
}
Please be aware of typeó since I have not tested this, but it should work
Yes, that worked! The problem was the BlockController as you said.
I added the route and removed the block, decided to use the button in my layout file instead - less hassle.
Thank you for the quick answers Valdis and Henrik!
Regards,
Ludvig Flemmich
It is actually possible to post directly to a BlockController, although I would not recommend it if you have other options. By defining a custom route (e.g. in Global.asax) you can target your block controller like this:
routes.MapRoute(
"CustomLogoutRoute",
"logout",
new { controller = "LogoutBlock", action = "Logout" }
);
Hi, I'm fairly new to working with EPiServer in MVC and I'm having some issues with routing I think.
I'm trying to access an action from a controller in a custom block I made by using this call:
and the controller looks like this:
But the problem is that it doesn't enter the specified controller and gives me http://mypage.com/LogoutBlock/Logout as the url, leading to a 404 Not Found.
I've read a little about routing but I'm not quite sure how to go at it, any ideas how I can solve this?