Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 860 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Routing optional parameters in ASP.NET MVC 5

#1
I am creating an ASP.NET MVC 5 application and I have some issues with routing. We are using the attribute `Route` to map our routes in the web application. I have the following action:

[Route("{type}/{library}/{version}/{file?}/{renew?}")]
public ActionResult Index(EFileType type,
string library,
string version,
string file = null,
ECacheType renew = ECacheType.cache)
{
// code...
}

We only can access this URL if we pass the slash char `/` in the end of `url`, like this:

type/lib/version/file/cache/

It works fine but does not work without `/`, I get a `404` not found error, like this

type/lib/version/file/cache

or this (without optional parameters):

type/lib/version

I would like to access with or without `/` char at the end of `url`. My two last parameters are optional.

My `RouteConfig.cs` is like this:


public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapMvcAttributeRoutes();
}
}

How can I solve it? Make the slash `/` be optional too?



Reply

#2
Maybe you should try to have your enums as integers instead?

This is how I did it

public enum ECacheType
{
cache=1, none=2
}

public enum EFileType
{
t1=1, t2=2
}

public class TestController
{
[Route("{type}/{library}/{version}/{file?}/{renew?}")]
public ActionResult Index2(EFileType type,
string library,
string version,
string file = null,
ECacheType renew = ECacheType.cache)
{
return View("Index");
}
}

And my routing file

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// To enable route attribute in controllers
routes.MapMvcAttributeRoutes();

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

I can then make calls like

[To see links please register here]

[To see links please register here]

[To see links please register here]


or

[To see links please register here]

[To see links please register here]

[To see links please register here]


and it works fine...
Reply

#3
//its working with mvc5
[Route("Projects/{Id}/{Title}")]
public ActionResult Index(long Id, string Title)
{
return view();
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through