-1

I am getting some problem regarding HTTP 400 - Bad Request whenever I publish my project to the server but locally works fine. Whenever I clear my browser cookies it works fine for some moment and then start same error. As far my understanding, my cookies memory exceed maximum length. Microsoft Support to this issue which I couldn't understand. N.B: I load my menu from database though cookies. Here is my code for load menu:

        HttpContext.Current.Response.Cookies.Add(MenuC1);
        HttpContext.Current.Response.Cookies.Add(MenuC2);
        HttpContext.Current.Response.Cookies.Add(MenuC3);
        HttpContext.Current.Response.Cookies.Add(MenuC4);
        HttpContext.Current.Response.Cookies.Add(MenuC5);

        HttpContext.Current.Response.Cookies.Add(AloneC1);
        HttpContext.Current.Response.Cookies.Add(AloneC2);
        HttpContext.Current.Response.Cookies.Add(AloneC3);
        HttpContext.Current.Response.Cookies.Add(AloneC4);
        HttpContext.Current.Response.Cookies.Add(AloneC5);

        UserID["UserID"] = this.User_User.ID.ToString();
        UserName["UserName"] = this.User_User.UserName;

        HttpContext.Current.Response.Cookies.Add(UserID);
        HttpContext.Current.Response.Cookies.Add(UserName);

Layout:

<li class="header"> User Menu </li>
@{
HttpCookie MenuC1 = HttpContext.Current.Request.Cookies["Menu1"];
HttpCookie MenuC2 = HttpContext.Current.Request.Cookies["Menu2"];
HttpCookie MenuC3 = HttpContext.Current.Request.Cookies["Menu3"];
HttpCookie MenuC4 = HttpContext.Current.Request.Cookies["Menu4"];
HttpCookie MenuC5 = HttpContext.Current.Request.Cookies["Menu5"];
string Menu = MenuC1["Menu1"].ToString().ToLower() + MenuC2["Menu2"].ToString().ToLower() + MenuC3["Menu3"].ToString().ToLower() + MenuC4["Menu4"].ToString().ToLower() + MenuC5["Menu5"].ToString().ToLower();
string TopMenu = "", SubMenu = "", Menuitem = "", Method = "";
}

Is this portion of code cause this error or any other problem? If this method of load menu cause error then what should better way to load menu from database. Any suggestion to this problem is appreciable.

Parvez
  • 187
  • 1
  • 2
  • 18
  • 4
    Why in the world are you using cookies for that. Pass a model to a view containing the data you want to generate the menu (and if you want it in the layout, use `@Html.Action(...)` to call a sever method that returns a partial view. –  Mar 04 '18 at 05:54
  • this link maybe help you : https://stackoverflow.com/questions/37112367/size-of-the-request-headers-is-too-long – mehdi farhadi Mar 04 '18 at 05:54
  • @StephenMuecke If I use Partial view, Shouldn't this hit frequently database for each page load?? I don't have that much idea about partial. – Parvez Mar 04 '18 at 06:00
  • If you want to avoid hitting the db on each request, add the data in `Session` –  Mar 04 '18 at 06:00
  • Session automatically timeout after a sudden period of time. Then what will happen!!? Shouldn't menu disappear after that?? @StephenMuecke – Parvez Mar 04 '18 at 06:03
  • 1
    As always, you test if the data exists in `Session` and if not then you get it again –  Mar 04 '18 at 06:05
  • @StephenMuecke thanks for your suggestion. Let me check this out. – Parvez Mar 04 '18 at 06:08

1 Answers1

0

i think there is a limit to the size of an HTTP request. when you load your DB to cookies that data is send to the server with each request.

you should create the menu as in Razor or JavaScript

michael berezin
  • 1,146
  • 7
  • 8