0

ASP.NET MVC application in IIS without port number added in default web site but not hitting the action methods and getting 404 error.

As my client asked to ignore port number in iis hosted website, I have added an application in Default Web Site. So, it got successfully working with landing/home page. But on the home page, I have a dropdownlist where the change event occurs for javascript by calling ajax with a jsonresult action in ASP.NET MVC controller.

When dropdownlist changes, the error hitting as domain/controller/actionmethod not found 404.

error

In the jQuery ajax call, I have used url home/actionmethod. As I hosted in default web site, my home page url is domain/appname. appname is the application name which i have added in default web site. With port number as domainname:8282(any port number) is working perfectly as follows:

works fine here

But after adding in default site which takes port number as 80, is not at all working. Code is here.

ajax call

action method

Any suggestions would be highly appreciated.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
MsDotNet raja
  • 15
  • 1
  • 8
  • 1
    One thing that stands out, your `TileAccessUpdate` method doesn't have the `[HttpPost]` attribute on it but you are making a POST request from client side JavaScript. – quaabaam Jun 01 '23 at 19:35
  • Does it hit in local environment? Could you please share controller full snippet instead of screenshot? – Md Farid Uddin Kiron Jun 05 '23 at 06:38
  • It is not hitting controller, as I gave alias name as tileaccess in default web site of iis. So, home page is loading url as domainname/tileaccess. I hope it is searching for domainname/tileaccess/controller name/actionname. But the format is only domainname/controller/action right? I am worrying how to fix it. – MsDotNet raja Jun 05 '23 at 11:41
  • @quaabam even after putting http post to action method, it is not working. – MsDotNet raja Jun 05 '23 at 11:42
  • Thanks for your response, please try the suggested steps provided below. – Md Farid Uddin Kiron Jun 06 '23 at 09:44

1 Answers1

1

ASP.NET MVC application in IIS without port number added in default web site but not hitting the action methods and getting 404 error.

Actually, based on your shared code snippet and description I have tried to simulate your scenario and ended with expected result.

I have following project structure:

enter image description here

Controller:

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [Route("Home/TitleAccessUpdate")]
        public JsonResult TitleAccessUpdate(int? Id)
        {
            return Json($"Access Id {Id} has been updated!");
        }
    }

View:

<strong><span id="appendHere"></span></strong></>
<hr />

<input id="accessId" />
<button type="button" name="submitButton" value="Edit" class="btn btn-primary"
        onclick="GetAjaxResponse()">
    Submit Request
</button>



@section scripts {
    <script>
        function GetAjaxResponse() {

            var idToUpdate = document.getElementById("accessId").value;
            console.log(idToUpdate);

            var localUrl = "https://localhost:7251/Home/TitleAccessUpdate";
            var publishUrl = "https://mypublishdapp.azurewebsites.net/Home/TitleAccessUpdate";

            $.ajax({
                type: "POST",
                url: localUrl,
                data: "Id=" + idToUpdate,
                dataType: "json",
                success: function (data) {
                    $("#appendHere").html(data);
                },
                error: function (xhr, status, error) {
                    alert('Data was not sent to backend!');
                }
            });
        }
    </script>
}

Simulation Test Local & Server:

enter image description here

enter image description here

What to double check:

First of all, please double check your projct working in local environment as expected. I have tried same as yours and working as expected.

Most importantly, your IIS port should not be the hurdle in this scenario. On top of this, try to call that URL from postman, if the URL correct then you should get response from postman if not then your published app URL might causing the issue. You can try both without defining the port and without th port number.

Furthermore, In my test both working fine. Based on your information it seems that your URL is not correct and please double check it if the port is not used by other program in that scenario, your app might have a port conflic issue.

Note: You can refer to this sample for more details. In addition, please check how to configure IIS for asp.net core project.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Hai Md Farid Uddin Kiron ji, I have tried whole as you shared. But still I am getting the same errors. Loading page url is zsapmobdbdev1/tileaccess. Then now, for action method, I am calling Home/TileAccessUpdate as controller and action method. So, as after first forward slash it calls the controller, it is unable to take zsapmobdbdev1/tileaccess/Home/TileAccessUpdate. Its just my assumption. I have written redirect code in routeconfig also, mentioned the routeattribute upon actionmethod, still nothing works as expected. I have screenshots of all, will update in question. Pls check. Thanks – MsDotNet raja Jun 06 '23 at 13:03
  • I am unable to upload images there. Here I am sharing the images links. https://ibb.co/gPpQVW2 https://ibb.co/5TQw1Bz https://ibb.co/Sfrcdvq https://ibb.co/Yp4tHFc – MsDotNet raja Jun 06 '23 at 13:14
  • Actually, your shared image does not help, first share your project structure [`like this`](https://i.stack.imgur.com/g7cRo.png) and try to call it from postman as I said earlier. In addition, you haven't shared if that working in local if so what's the request URL, based on your project structure need to check next steps. – Md Farid Uddin Kiron Jun 07 '23 at 00:52
  • Here I am sharing my project structure image with local host working screenshot. https://imgtr.ee/images/2023/06/07/pD4tV.jpg https://imgtr.ee/images/2023/06/07/pDcNm.jpg – MsDotNet raja Jun 07 '23 at 12:05
  • As it is taking url as zsapmobdbdev1/tileaccess as home page url, I have created another application in default website as -tileaccess. Still it is taking zsapmobdbdev1/-tileaccess and unable to hit controller home and action method tileaccess update. Here are the screenshots. https://imgtr.ee/images/2023/06/07/pKTxF.jpg https://imgtr.ee/images/2023/06/07/pKm0U.jpg – MsDotNet raja Jun 07 '23 at 12:13
  • Rather than this, tell me onething clearly. My requirement is just to host my mvc app in iis, and when browse, I should not get port number. Thats all. Please help me with this. I tried in multiple ways, its hitting me error or not hitting all the methods properly. I need just this with all functions to hit . – MsDotNet raja Jun 07 '23 at 17:13
  • Alright, port should not get if you can configure correctly, try to set another port other than 80 because 80 might be in use by other program. Please [check the steps here](https://stackoverflow.com/questions/75527015/how-do-i-host-a-net-core-application-on-iis/75528784#75528784) let me know if you are still having trouble. – Md Farid Uddin Kiron Jun 08 '23 at 01:37
  • Sir, I am not using asp.net core. Its just a simple app as you have seen my project screenshot previously uploaded. Simple asp.net mvc app. Also, my team lead said not to install anything in the remote server. So, shall I just check what you have mentioned? Is that okay? – MsDotNet raja Jun 08 '23 at 03:08
  • Okay got it but you don't need to inastall anything. – Md Farid Uddin Kiron Jun 08 '23 at 03:15
  • Sir, finally got the solution. But not as you suggested. As I have mentioned earlier error that it is saying zsapmobdbdev1/home/TileAccessUpdate is 404 not found. I have added in javascript as hostname/tileaccess/controller/actionmethod , because home page is loading as hostname/tileaccess as I have added the site under default site. So wherever I am calling the action method, I have mentioned that host url dynamically. It resolved. Thank You for your patience and time. – MsDotNet raja Jun 12 '23 at 12:23
  • Thanks for your response, glad to hear that that it resolved. – Md Farid Uddin Kiron Jun 12 '23 at 14:21