Ashan
Ashan

Reputation: 1

ASP.NET Core action method not returning the id up to 2 digits(i.e 1, 22, 66) but return 3 digit(i.e 111, 222, 666)

I am have an action method Contact with return type (int) and taking (int id) as parameter.

The problem is that on this route when I pass two digit id, but it not shows, but it shows the id with three digits such as (111) but not showing (1)

enter image description here when I passing id = 1 it is not showing

enter image description here when I passing id = 111 it is showing

HomeController.cs

using _10_TagHelpersDemo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace _10_TagHelpersDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public int Contact(int id)
        {
            return id;
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

Program.cs

namespace _10_TagHelpersDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddControllersWithViews();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();
        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22515

I have attached the the main file can you please check my middlewares (if you know which one is creating problem), it is the by default ASP .NET CORE MCV template's main Program.cs file

Based on your shared code snippet and then after with the full program.cs file code, I have tried to simulate your issue. But its working exactly as expected.

I have investigated with 1 digit, 2 digits and more, all the cases produced the correct output.

In addition, the middleware code with the details of program.cs files also seems correct.

Here is the output of the investigation:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Note: According to your controller and program.cs file everything seems correct and I haven't got any issue while testing your scenario. Would you kindly double check if anything else you have which has not been shared. If you are checking output in browser, please clear the browser cache or check if anything other value is causing trouble.

Upvotes: 0

Related Questions