volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

EPiServer MVC Routing(?) issues

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:

@using (Html.BeginForm("Logout", "LogoutBlock", FormMethod.Post))
    {
        
    }




and the controller looks like this:

       

[HttpPost]
        public void Logout()
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            UrlHelper url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
            Response.Redirect(UrlHelpers.PageLinkUrl(url, PageReference.StartPage).ToHtmlString());
           
        }



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?

#90205
Sep 03, 2014 15:15
Vote:

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)
#90210
Sep 03, 2014 19:26
Vote:

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

#90222
Sep 04, 2014 8:40

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

#90227
Sep 04, 2014 9:06

Glad that it worked!

Your welcome

#90229
Sep 04, 2014 9:10

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" }
);
#90246
Sep 04, 2014 12:33
error This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.