0

I have a simple phonebook app that allows the user to search for an employee via a dropdown list of departments or by name via text input. The way I'm handling the search is simply changing the action attribute of the html form with javascript based on whatever they typed or whichever option they selected in the dropdown:

function selectedOption(option) {
    var selValue = option.value;

    document.getElementById("search-form").action = "/home/index/" + selValue;
}

This works on localhost but when I host it on IIS:

machineName/appName

becomes

machineName/home/index/selValue

cutting off the app name and returning a 404 error

The only way I've been able to get around this is with some hardcoding to check whether "home/index" exists in the path already...

function selectedOption(option) {
    var selValue = option.value;

    if (window.location.href.includes("home")) {
        var searchDest = selValue
    } else {
        var searchDest = window.location.href + "/home/index/" + selValue
    }

    document.getElementById("search-form").action = searchDest;
}

This works but it is not very clean or conventional. Is there some way I can have the app name configured in Startup.cs? I saw the same issue in another question here but I'm getting syntax errors when I try to add another MapRoute. This is all that I currently have:

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

Any idea how to get around this?

bfitz22
  • 1
  • 1
  • Try excluding the leading slash, ie "home/index/" – pinkfloydx33 May 18 '20 at 22:43
  • I tried that already and it works the same way but in reverse. The first search works and subsequent searches do not as every search appends a new "home/index" to the end of the existing url – bfitz22 May 18 '20 at 23:04
  • you can try ```document.getElementById("search-form").action = '<%= Url.Content("~/") %>' + "/home/index/" + selValue;```. `Url.Content` will generate a url relative to your application. https://stackoverflow.com/questions/922997/asp-net-mvc-resolve-urls-in-javascript/923024 – Desmond Chin May 19 '20 at 08:41
  • I might be wrong but that looks like erb syntax. I'm in a cshtml file so that doesn't work. Anyway, I could do the same thing with window.location.href but I want to avoid coding like that because I don't think it's best practice. Thanks for the contribution though! – bfitz22 May 19 '20 at 17:29
  • For cshtml, you can try `@Url.Content("~/home/index/")` – Desmond Chin May 21 '20 at 05:48

1 Answers1

0

as mentioned above by @Desmond Chin, for cshtml you can use HTML helpers. I think the best one for that case is to use @Url.Action(actionName, controllerName) instead @Url.Content() - which is used for js, css, img, etc..

You can use this code bellow to achieve this:

function selectedOption(option) {
    var selValue = option.value;

    document.getElementById("search-form").action = '@Url.Action("Index", "Home")?<<VARIABLENAMEHERE>>'+selValue;

}

The Html Helper will take care of your url, virtual paths and etc

iganja
  • 19
  • 7