-1

It is possible to create URLs like this in asp.net MVC?

1.

http://url/Home/Function1?name=tomek -> http://url/my-name-tomek
or
http://url/Home/Function1?name=john -> http://url/my-name-john

2.

http://url/Home/Function2?name=tomek&address=london -> http://url/my-name-tomek/my-address-london
or
http://url/Home/Function2?name=john&address=york -> http://url/my-name-john/my-address-york
Tazani
  • 39
  • 6
  • Asp.net does not have controllers. Did you mean MVC? – stuartd Apr 15 '16 at 14:18
  • 3
    Can you show what you've tried so far? – stuartd Apr 15 '16 at 14:27
  • Please check the examples. – Tazani Apr 19 '16 at 13:04
  • You can keep deleting and re-adding that comment for ever if you like, but as well as the examples of what you want to do, we need to see what you have tried to do yourself to solve the problems, ie the routes you have tried. "Is it possible?" on it's own will never get an answer. "Why doesn't this code do what I want?" might. – stuartd Apr 19 '16 at 17:25
  • Not unless your app has only one controller (`HomeController`) with one method (`Function1(string name, string address)`) –  Apr 20 '16 at 05:46
  • Routing is for mapping a URL (a path only, without query string parameters) to a resource (controller, action, and other route values). As such, your question doesn't really make any sense in the context of routing, because routing is not for mapping one URL to another. It would make sense if what you are requesting is to add HTTP redirects or URL rewrites for the specified cases, but it is not clear that is what you are asking. And if so, this question is not about routing at all. – NightOwl888 May 11 '16 at 16:14
  • @StephenMuecke Why do you need one method? – Erick Asto Oblitas Sep 07 '18 at 16:36
  • @ErickAstoOblitas, OP wants to omit both the controller and action name, which means OP can have only one controller and method, or they must have a complex route constraint (similar to [Routing in ASP.NET MVC, showing username in URL](https://stackoverflow.com/questions/37358416/routing-in-asp-net-mvc-showing-username-in-url/37359345#37359345)). (note your answer includes a `/MyFunction/` prefix which is not what OP asked for, albeit it makes sense to include a prefix) –  Sep 08 '18 at 05:34

1 Answers1

0

That is not 'advanced routing'.

  1. In RouteConfig:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes(); // <- with this line you enable attribute routing
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    
  2. In your Controller

    namespace TestingUrl.Controllers
    {
        [RoutePrefix("MyController")] // <- Put a special url name for the controller
        public class AnotherController : Controller
        {
            [Route("MyFunction/{name}/{city}")] // <- A special name for the action and the two parameters
            [HttpGet]
            public ActionResult AnotherIndex(string name, string city)
            {
                ViewBag.Name = name;
                ViewBag.City = city;
                return View();
            }
        }
    }
    
  3. Tested with this urls and worked:

    3.1. localhost:port/MyController/MyFunction/my-name-Andres/Arequipa

    3.2. localhost:port/MyController/MyFunction/my-name-Rahim/Islamabad

Erick Asto Oblitas
  • 1,399
  • 3
  • 21
  • 47