12
votes

I have the Home Controller and my Action name is Index. In My route config the routes like below.

routes.MapRoute(
    "Default",   // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
);

Now I call my page like http://localhost:11045/Home/Index is correct.

If I call my page like following it should redirect to error page.
localhost:11045/Home/Index/98 or
localhost:11045/Home/Index/?id=98.

How to handle this using routing attribute.

My Action in Controller look like below.

public ActionResult Index() 
{
  return View(); 
}
4
Please edit and clarify your question. What do you mean by "is right" and "is wrong"? What URLs do you need to work and what URLs do you need to block? Are you getting an error in the "is wrong" case? If so, what is it? Also, based on your title, it is unclear whether the question is about attribute routing or convention-based routing (convention-based being where your default route normally is).NightOwl888
Can you please mark as answered if you satisfied, please ? If you still have any question please add your comments heremonikapatelIT

4 Answers

31
votes

For Attribute Routing in ASP.NET MVC 5

decorate your controller like this

[RoutePrefix("Home")]
public HomeController : Controller {
    //GET Home/Index
    [HttpGet]
    [Route("Index")]
    public ActionResult Index() {
        return View(); 
    }
}

And enable it in route table like this

public class RouteConfig {

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

        //enable attribute routing
        routes.MapMvcAttributeRoutes();

        //convention-based routes
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = "" }
        );
    }
}
1
votes

Please check here for information on routing: http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

Most likely, default routing should be something like below:

routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

Also, looks like the Index Action Method is missing a parameter, see below:

public ActionResult Index(string id)
        {
            return View();
        }

Try placing string id in your Index method.

1
votes
public class URLRedirectAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
                string destinationUrl = "/VoicemailSettings/VoicemailSettings";
                filterContext.Result = new JavaScriptResult()
                {
                    Script = "window.location = '" + destinationUrl + "';"
                };
        }
    }
0
votes

try changing the index action to this:

public ActionResult Index(int? id = null) 
{
  return View(); 
}

This should do the trick. So you can pass the id as a param with the "/{value}" or just use "/?id={value}"