Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, my current url looks like this.

http://localhost:57208/bilar/index?state=nya[^]
i want it to look like this

http://localhost:57208/bilar/nya[^]

how can i solve this?

What I have tried:

i have tried attribute routing like this
[Route("/bilar/{state}")]
but it doesnt work
Posted
Updated 16-Jan-20 22:56pm
v4
Comments
Richard MacCutchan 17-Jan-20 4:22am    
Your links point to localhost which is your PC so no one else can get to them.

1 solution

Hello,

disable EnableEndpointRouting on ConfigureServices and test again.

services.AddMvc(opt=>{opt.EnableEndpointRouting=false; });


sample Startup.cs

namespace CoreMVC.Test
{
    public class Startup
    {
        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.AddControllersWithViews();
           services.AddMvc(opt=>{opt.EnableEndpointRouting=false; });
           
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

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


Sample Controller

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

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

        [Route("")]
        [Route("bilar")]
        [Route("bilar/Index")]
        public IActionResult Index()
        {
            return View();
        }
        [Route("bilar/{state}")]
        public IActionResult Privacy(string state)
        {
            return View();
        }        
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900