The URL requested was not found on the server - ASPNET.CORE

sblb 1,231 Reputation points
2022-06-07T20:59:38.473+00:00

Hi,
I tried to do an application aspnet core with jquery datable. The data is in file table in sql sever.
The path of server is : \\servername\\PartageData\\wSerie, with wSerie the database. The path of the file is \\servername\\PartageData\\wSerie\wSerieDir

When I run my application I have error message

404 - Not Found.
The URL requested was not found on the server.
Check for typos in the file name in the ajax parameter and in your file on the server.

I thought that the problem is with my definition of the connectionStrings because all files is in wSerieDir; but I tried to change it by Catalog=wSERIE\\wSerieDir but I've always 404-Not Found!
Have you an idea?


I defined appsettings as below

  "ConnectionStrings": {
    "DefaultConnection": "Data Source=servername\\PartageData;Initial Catalog=wSERIE;Trusted_Connection=True"

  },

I use the class

 public class wSerie
    {
        public Guid stream_id { get; set; }
        public Byte[]? file_stream { get; set; }
        public string? name { get; set; }
        [NotMapped]
        public HierarchyId? path_locator { get; set; }
        [NotMapped]
        public HierarchyId? parent_path_locator { get; set; }
        public string? file_type { get; set; }
        public Int64 cached_file_size { get; set; }
        public DateTimeOffset creation_time { get; set; }
        public DateTimeOffset last_write_time { get; set; }
        public DateTimeOffset last_access_time { get; set; }
        public bool is_directory { get; set; }
        public bool is_offline { get; set; }
        public bool is_hidden { get; set; }
        public bool is_readonly { get; set; }
        public bool is_archive { get; set; }
        public bool is_system { get; set; }
        public bool is_temporary { get; set; }
    }

ApplicationDbContext.cs

public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        { }

        public DbSet<wSerie> wSeries { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<wSerie>().HasNoKey().ToTable("wSerie", t => t.ExcludeFromMigrations());
            base.OnModelCreating(modelBuilder);
        }
    }

I define the API

 [Route("api/[controller]")]
    [ApiController]
    public class wSerieController : ControllerBase
    {
          private readonly ApplicationDbContext context;
          public wSerieController(ApplicationDbContext context)
          {
              this.context = context;
          }

         [HttpPost]
            public IActionResult GetSerie()
            {
                try
                {
                    var draw = Request.Form["draw"].FirstOrDefault();
                    var start = Request.Form["start"].FirstOrDefault();
                    var length = Request.Form["length"].FirstOrDefault();
                    var sortColumn = Request.Form["columns[" + Request.Form["order[0][column]"].FirstOrDefault() + "][name]"].FirstOrDefault();
                    var sortColumnDirection = Request.Form["order[0][dir]"].FirstOrDefault();
                    var searchValue = Request.Form["search[value]"].FirstOrDefault();
                    int pageSize = length != null ? Convert.ToInt32(length) : 0;
                    int skip = start != null ? Convert.ToInt32(start) : 0;
                    int recordsTotal = 0;
                    var customerData = (from tempcustomer in context.wSeries select tempcustomer);
                    if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDirection)))
                    {
                        customerData = customerData.OrderBy(sortColumn + " " + sortColumnDirection);
                    }
                    if (!string.IsNullOrEmpty(searchValue))
                    {
                    customerData = customerData.Where(m => m.name.Contains(searchValue)
                                                     ||  m.file_type.Contains(searchValue) 

                                                     );
                    }
                    recordsTotal = customerData.Count();
                    var data = customerData.Skip(skip).Take(pageSize).ToList();
                    var jsonData = new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data };
                    return Ok(jsonData);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }

    }
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
{count} votes

Accepted answer
  1. Jaliya Udagedara 2,836 Reputation points MVP Volunteer Moderator
    2022-06-08T09:50:13.743+00:00

    In your Startup.cs, you haven't mapped Controllers in the request pipeline.

    Do this,

       app.UseEndpoints(endpoints =>  
       {  
          // Missing below line  
          endpoints.MapControllers();  
         
          endpoints.MapRazorPages();  
       });  
    

    And the endpoint you are calling is correct. According to you code, it should be: /api/wSerie (just like you have it already)

    On a separate note, consider following standard coding conventions: C# Coding Conventions


5 additional answers

Sort by: Most helpful
  1. sblb 1,231 Reputation points
    2022-06-08T08:15:12.583+00:00

    here ajx code

    $(document).ready(function () {  
        $('#customerDatatable').dataTable({  
            "processing": true,  
            "serverSide": true,  
            "filter": true,  
            "ajax": {  
                "type": "POST",  
                "url": "/api/wSerie",  
                "datatype": "json"  
            },  
            "columnDefs": [{  
                "targets": [0],  
                "visible": false,  
                "searchable": false  
            }],  
            "columns": [  
                { "data": "name", "name": "name", "autoWidth": true },  
                { "data": "file_type", "name": "file_type", "autoWidth": true },  
                                        
            ]  
      
             });  
    });  
    

    I use razor page template.
    I define the Startup.cs as below

       public void ConfigureServices(IServiceCollection services)  
            {  
                services.AddDbContext<ApplicationDbContext>(options =>  
                                        options.UseSqlServer(  
                                            Configuration.GetConnectionString("DefaultConnection"),  
                                            b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));  
      
                services.AddScoped<IFileRepository, FileRepository>();  
      
                services.AddControllers();  
                services.AddRazorPages();  
                services.AddSwaggerGen();  
      
            }  
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
    {...  
                    app.UseEndpoints(endpoints =>  
                    {  
                        endpoints.MapRazorPages();  
                    });  
        ..}  
    

  2. sblb 1,231 Reputation points
    2022-06-08T15:13:14.357+00:00

    I think the problem comes from the columns definition from datatable

    because in wSeriecontroller in define : var customerData = (from tempcustomer in context.wSeries select tempcustomer);
    this means the whole data from wSeries will selected.
    So, in the customerDatable the definition of column must identique to the definition of wSerieController.

    What do you think?
    I will tried it and I come back!

    0 comments No comments

  3. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-06-08T15:35:03.65+00:00

    what is the full url (in the browser) of the page hosting the Ajax?


  4. sblb 1,231 Reputation points
    2022-06-09T08:59:51.973+00:00

    I suggest you could try to put a breakpoint at the wSerie method, to see if the client has fired the wSerie method.

    I've put a breakpoint and the client has not fired the wSerie method.

    Do you have some other exception handler which will be fired when catching the exception inside the method?

    No other exception

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.