在其他机器上得到 404,为啥?

Posted

技术标签:

【中文标题】在其他机器上得到 404,为啥?【英文标题】:Getting 404 on other machine, Why?在其他机器上得到 404,为什么? 【发布时间】:2020-10-29 20:47:40 【问题描述】:

我正在开发一个 Asp.Net Core 3.1 API,当我们从 Visual Studio 本地运行 API 时,在我的机器以及其他开发人员的机器上一切正常。我们能够满足请求并获得正确的响应。

但另一位开发人员刚刚克隆了 repo 并使用 git pull 获取了最新代码。所以他有相同的源代码。

当他从 Visual Studio 本地运行项目并尝试访问 HTTP GET 方法时,他收到 HTTP 404 错误。

他能够调用一个控制器的方法,而对于其他控制器的 HTTP GET 方法,他得到 404 错误。并且 URL 是有效的。

我在控制器上也添加了AllowAnonymous,浏览器控制台除了404错误也没有其他信息。

那么可能是什么问题?

更新:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using FxBatchProcCommon.Logger;
using FxBatchProcessing.InstructionValidation.Interfaces;
using FxBatchProcessing.InstructionValidation.Rules;
using FxBatchProcessing.Payment.Interfaces;
using FxBatchProcessing.Payment.Xml;
using FxBatchProcessing.Repository;
using ISOXMLValidationApi.Repositories;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;

namespace ISOXMLValidationApi

    public class Startup
    
        private const string _apiVersion = "v1";

        public Startup(IConfiguration configuration)
        
            Configuration = configuration;
        

        public IConfiguration Configuration  get; 

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        
            services.AddControllers();

            services.AddScoped<IISOXMLValidationRepository, ISOXMLValidationRepository>();
            services.AddScoped<IDBUtilRepository, DBUtilRepository>();
            services.AddScoped<IRuleManager, RuleManager>();
            services.AddScoped<IFieldRepository, FieldRepository>();
            services.AddScoped<IErrorRepository, ErrorRepository>();
            services.AddScoped<IBatchProcessorConfigRepository, BatchProcessorConfigRepository>();
            services.AddScoped<IPaymentCollectionConverter, PaymentCollectionConverter>();
            services.AddScoped<IInstructionRepository, InstructionRepository>();
            services.AddScoped<ICurrencyRepository, CurrencyRepository>();

            services.AddSwaggerGen(options =>
            
                options.SwaggerDoc("v1", new OpenApiInfo
                
                    Version = _apiVersion,
                    Title = "ISOXMLValidation API",
                    Description = "ISOXMLValidationApi"
                );
                options.DocInclusionPredicate((docName, description) => true);

                // Define the BearerAuth scheme that's in use
                options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
                
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Bearer token\"",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey
                );

                options.OperationFilter<SecurityRequirementsOperationFilter>();
            );

            services.AddAutoMapper(typeof(Startup));
        

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        
            loggerFactory.AddLog4Net();

            if (env.IsDevelopment())
            
                app.UseDeveloperExceptionPage();
            

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            
                endpoints.MapControllers();
            );

            // Enable middleware to serve generated Swagger as a JSON endpoint
            app.UseSwagger(c =>  c.RouteTemplate = "swagger/documentName/swagger.json"; );

            // Enable middleware to serve swagger-ui assets (html, JS, CSS etc.)
            app.UseSwaggerUI(options =>
            
                // specifying the Swagger JSON endpoint.
                options.SwaggerEndpoint($"../swagger/_apiVersion/swagger.json", $"MyProject API _apiVersion");
                options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests.  
            );
        
    



namespace ISOXMLValidationApi.Controllers

    [Route("[controller]")]
    [ApiController]
    [AllowAnonymous]
    public class ISOXMLValidationController : ControllerBase, Helper.ILogger
    
        //private readonly IConfiguration _configuration;
        //private readonly IISOXMLValidationRepository _iSOXMLValidationRepository;
        ILogger<Helper.ILogger> _logger;
        private IRuleManager _ruleManager;
        //private PaymentInfo _payment;
        //private IBatchProcessorConfigRepository _batchProcessorConfigRepository;
        //private IPaymentCollectionConverter _paymentCollectionConverter;
        public ISOXMLValidationController(/*IConfiguration configuration, IISOXMLValidationRepository iSOXMLValidationRepository,*/
            ILogger<Helper.ILogger> logger, IRuleManager ruleManager/*, PaymentInfo payment,*/
            /*IBatchProcessorConfigRepository batchProcessorConfigRepository, IPaymentCollectionConverter paymentCollectionConverter*/)
        
            //_iSOXMLValidationRepository = iSOXMLValidationRepository;
            _logger = logger;
            //_configuration = configuration;
            _ruleManager = ruleManager;
            //_payment = payment;
            //_batchProcessorConfigRepository = batchProcessorConfigRepository;
            //_paymentCollectionConverter = paymentCollectionConverter;
        
        /// <summary>
        /// Method to get all the validation rules for currency and country
        /// </summary>
        /// <param name="currencyCode"> default value ""</param>
        /// <param name="countryId">default value 0</param>
        /// <returns>all validate rules for currency and country</returns>
        [HttpGet]
        [Route("GetValidationRules")]
        public ActionResult GetValidationRules(string currencyCode = "", int countryId = 0)
        
            try
            
                _logger.LogInformation($"GetValidationRules currencyCode currencyCode countryId countryId");
                IEnumerable<IFieldRuleConfig> ccyConfigRules = _ruleManager.GetValidationRules(
                    new CurrencyCountry  CurrencyCode = currencyCode, CountryFk = countryId );

                return Ok(ccyConfigRules);
            
            catch (Exception ex)
            
                _logger.LogError(ex.ToString());
                return StatusCode(StatusCodes.Status500InternalServerError);
            

       

【问题讨论】:

你能把你的startup.cs和你的控制器代码放上去吗? 您是否缺少应用程序 URL 的 IIS 托管、虚拟目录映射或本地计算机的 HOST 条目? @ChetanRanpariya 它的代码完全相同,我在我的机器上使用,其他开发人员也使用相同的代码,没有做任何更改 代码相同,但运行代码的方式可能不同。 Web 项目是否配置为在 IISExpress 或某些 IIS 托管的 Web 应用程序上运行? @ArsalanValoojerdi 我已在问题中添加,请检查 【参考方案1】:

我已经解决了这个问题,但我仍然不知道为什么它不起作用。我正在写下面的解决方法,如果有人知道导致问题的原因,请告诉我?

所以在其他机器开发人员正在运行 Visual Studio 2017 的解决方案,所以我使用 Visual Studio 2019 运行它,然后它开始按预期工作

但在那之后,我尝试在我的机器上使用 Visual Studio 2017 运行该项目,并且它在我的机器上按预期工作。

    在他的机器上,它在 VS 2019 上按预期工作,但在 VS 中却没有 2017 年。 但在我的机器上,它在 2019 年和 2017 年都可以工作。

所以请让我知道根本原因是什么? 以及如何让它在他的机器上按照 VS 2017 中的预期工作?

【讨论】:

以上是关于在其他机器上得到 404,为啥?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 404 在 django url 中?

为啥ubuntu12.04下mysql数据库联不了其他机器

为啥我在这个非常简单的 MySQL 查询上得到文件排序?

为啥 VB6 MkDir 命令在其他机器上抛出错误 75 而在我的机器上却没有?

为啥我的 javascript 文件是唯一获得 404 的文件? [复制]

网站为啥会出现404错误,如何解决