Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

model view controller - How to access ASP .NET MVC URL?

I am doing code by watching Mosh ASP .Net tutorial. But I stack on a thing. My routing url is not working but query string is working.

Here is my code:

routing code

routes.MapRoute(
                "MovieByReleaseDate",
                "movie/released/{year}/{month}",
                new {controller = "Movie", action = "ByReleaseDate" }
                );

action code

public ActionResult ByReleaseDate(int year, int month)
        {
            return Content(year+"/"+month);
        } 

but link is not working. screenshot added http://localhost:49941/Movie/ByReleaseDate/2015/12

but link is not working. screenshot added

working with query working this only screenshot added

question from:https://stackoverflow.com/questions/65940447/how-to-access-asp-net-mvc-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you not defining a proper routing, you won't accomplish your goal. Looks like you need a better understanding on routing.

Please refer to https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

You are not following the convention in those cases where you want to use explicit routing instead of using the query string.

Try adding the default values into the route definition.

new {controller = "Movie", action = "ByReleaseDate" ,year=0, month=0 }

Basically when you defining routings you need to define the way that you will receive the information.

new {controller = "Movie", action = "ByReleaseDate" }
Will allow you to route below patterns

  • {controller}/{action}
  • {controller}/{action}? "query string"

new {controller = "Movie", action = "ByReleaseDate",year=0 }
Will allow you to route below patterns

  • {controller}/{action}/year
  • {controller}/{action}? "query string"
  • {controller}/{action}/year? "query string"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...