How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

huangapple go评论56阅读模式
英文:

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

问题

在IIS中运行的ASP.NET MVC应用程序,没有在默认网站中添加端口号,但无法访问操作方法并出现404错误。

由于客户要求在IIS托管的网站中忽略端口号,我已经在默认网站中添加了一个应用程序。因此,着陆页/主页可以正常工作。但是在主页上,我有一个下拉列表,当下拉列表发生更改时,通过调用ASP.NET MVC控制器中的jsonresult操作来调用javascript的更改事件。

当下拉列表更改时,出现错误,提示域/controller/actionmethod未找到404。

在jQuery的ajax调用中,我使用了url home/actionmethod。由于我托管在默认网站中,我的主页URL是domain/appname。appname是我在默认网站中添加的应用程序名称。在带有端口号的域名domainname:8282(任何端口号)下,一切正常工作,如下所示:

但是在添加到将端口号设置为80的默认站点后,根本不起作用。以下是代码:

任何建议将不胜感激。

英文:

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.

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

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:

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

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

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

Any suggestions would be highly appreciated.

答案1

得分: 1

以下是您要翻译的部分:

在IIS中的ASP.NET MVC应用程序中,没有在默认网站中添加端口号,但无法访问操作方法,出现404错误。
实际上,根据您提供的代码片段和描述,我已经尝试模拟了您的情况,并获得了预期的结果。
我有以下项目结构:
控制器:

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!");
    }
}

视图:

<strong><span id="appendHere"></span></strong>
<hr />
<input id="accessId" />
<button type="button" name="submitButton" value="Edit" class="btn btn-primary"
    onclick="GetAjaxResponse()">
    提交请求
</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('数据未发送到后端!');
                }
            });
        }
    </script>
}

本地和服务器的模拟测试:
需要双重检查的事项:
首先,请确保您的项目在本地环境中按预期工作。我尝试了与您相同的操作,结果如预期。
最重要的是,您的IIS端口不应该是这种情况的障碍。此外,请尝试从Postman中调用该URL,如果URL正确,那么您应该从Postman中获得响应;如果不正确,那么您发布的应用程序URL可能会导致问题。您可以尝试在不定义端口和不使用端口号的情况下都进行尝试。
此外,在我的测试中,两者都正常工作。根据您的信息,似乎您的URL不正确,请双重检查一下,如果在该情况下端口未被其他程序使用,那么您的应用程序可能存在端口冲突的问题。
注意: 您可以参考此示例以获取更多详细信息。此外,请查看如何配置IIS以进行ASP.NET Core项目的文章。

英文:

> 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:

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

Controller:

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

View:

&lt;strong&gt;&lt;span id=&quot;appendHere&quot;&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/&gt;
&lt;hr /&gt;

&lt;input id=&quot;accessId&quot; /&gt;
&lt;button type=&quot;button&quot; name=&quot;submitButton&quot; value=&quot;Edit&quot; class=&quot;btn btn-primary&quot;
        onclick=&quot;GetAjaxResponse()&quot;&gt;
    Submit Request
&lt;/button&gt;



@section scripts {
    &lt;script&gt;
        function GetAjaxResponse() {

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

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

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

Simulation Test Local & Server:

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

How to fix ASP.NET MVC action method not found 404 error in IIS hosted website without port number in URL when calling via jQuery ajax?

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.

huangapple
  • 本文由 发表于 2023年6月2日 03:21:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定