0

I click a button in my webapp and the page goes to the url "https://localhost:44321/Home/Generate". When I click the button a second time the page goes to the url "https://localhost:44321/Home/Home/Generate" and I get an error because the page is not found.

I have a button with the following code:

<form method="post" action="Home/Generate">
    <input type="submit" value="Generate" />
</form>

I have a Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvcWithDefaultRoute();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}");
    });

}

and I also tried to add:

routes.MapRoute(
    name: "generate",
    template: "{controller=Home}/{action=Generate}");

In my HomeController I call the method after a click like this:

[HttpPost]
public IActionResult Generate()
{
    bool result = false;
    if (result) return RedirectToAction("Window", new { param = order.ID.ToString() });
    else return RedirectToAction("Window", 0);
}

public IActionResult Window(string param)
{
    if (param == "NotReady")
    {
        ViewBag.JavaScriptFunction = string.Format("ShowNotReady();");
    }
    else
    {
        ViewBag.JavaScriptFunction = string.Format("ShowError();");
    }
    return View("Index");
}

public IActionResult Index()
{
    return View();
}

What I have to do, that "https://localhost:44321/Home/Generate" is the url after every button click, not only after the first click?

picolino
  • 4,856
  • 1
  • 18
  • 31
  • 1
    add a slash before home : `/home/generate` (or better still a tilda and a slash: `~/home/generate` - the tilda will add your app name in if you are running under an app) - otherwise you are using a relative path (which will go from the current location in your url) – Pete Dec 11 '19 at 10:21
  • You're welcome, happy to help :) – Pete Dec 11 '19 at 10:24
  • @Pete: Please add that as an answer so the OP can accept it. – Chris Pratt Dec 11 '19 at 13:53
  • @ChrisPratt I voted to close as a typo – Pete Dec 11 '19 at 14:02
  • That's fine, but there's no guarantee that the question will be closed or when that may occur. In the meantime, it remains in the unanswered queue. The close as typo is more about pruning questions from the site that are not likely to ever benefit anyone else, but if there's an answer and you're willing to take the time to give it, then it might as well actually be as an answer, instead of a comment. – Chris Pratt Dec 11 '19 at 14:10

1 Answers1

2

You have used a relative path for your url in the form action. This means that every time the form is submitted, it will add the action onto the current url, giving you the behaviour you are seeing.

In order to ensure your url starts from home every time the form is submitted, you need to add a slash before the url:

<form method="post" action="/Home/Generate">
    <input type="submit" value="Generate" />
</form>

As this is an asp.net mvc application, you could also add a tilda before the slash:

~/Home/Generate

This will ensure that if you are running the site as an application of another site, it will always include the app path first in the url

More information about different types of URL

Pete
  • 57,112
  • 28
  • 117
  • 166