2

Introduction

I am working on demo application where users can register.Users table have "username" field.There is "detail" action in "home" controller accepting parameter of string "username".

Code

public ActionResult Detail (string username)
{
 return View();
}

Then url will be

www.example.com/home/Detail?username=someparam

Problem

Can i setup route like that ?

www.example.com/someparam

If its possible, then please let me know. Any kind of help or reference will be appreciated.

Thanks for your time.

Suhail Mumtaz Awan
  • 3,295
  • 8
  • 43
  • 77
  • 1
    Unless you create a route constraint that looks up the database each time any request is made (which would result in poor performance), then you cant. –  Jul 09 '16 at 07:06
  • @StephenMuecke thanks for reply, i understand the performance issue.Should i include atleast action ? – Suhail Mumtaz Awan Jul 09 '16 at 07:10
  • 1
    Yes, you could create a route definition such as `url: Detail/{username}"` that defaults to the `Detail()` method in `HomeController` (or it could be `Users/{username}` or anything that makes it unique) –  Jul 09 '16 at 07:14
  • @StephenMuecke i added rout with url: "{username}" with non-nullable. it worked. we will need to check if user exist otherwise redirect. Can you please suggest if its wrong way or ok ... Thanks – Suhail Mumtaz Awan Jul 09 '16 at 07:52
  • 1
    If you want to do that, then you need a route constraint that checks if the parameter is a user (by looking up a repository of users) and if its not a user, return `false` so that the next route is then searched. –  Jul 09 '16 at 07:56
  • @StephenMuecke Thanks for help, i will setup this constraint, any reference help to search ? – Suhail Mumtaz Awan Jul 09 '16 at 08:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116858/discussion-between-stephen-muecke-and-suhail-mumtaz-awan). –  Jul 09 '16 at 08:05

2 Answers2

3

That is doable if you change the way your routes are defined. Let's assume that you use dot net 4.5.2

Have a look in RouteConfig under App_Start.

A typical route definition is defined like this :

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

Nothing stops from changing the route to look like this :

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

);

We are basically hardcoding the controller and action to be a certain value so now you could just have url/paramname and it will hit the hardcoded combination.

This being said I would not do things like this as it's against the way MVC works

MVC routes are url/controller/action. You can skip the action for generic stuff like an Index for example and your URL becomes url/controller. MVC needs to be able to identify which controller you want to hit and which action and it's best to stay within the conventions it has.

Plus, each application will typically have more than one controller, which allows a nice Separation of Concerns. Now you've hardcoded yourself ito having just one controller and action.

What you are suggesting can be done a lot easier in a webforms manner though so maybe you want to look into that.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
1

If you define code like this

[HttpGet, Route("api/detail/{username:string}")]
public ActionResult Detail (string username)
{
 return View();
}

Then url will be

www.example.com/api/Detail/someparam

So I guest you define as following, please try with your own risk!

[HttpGet, Route("/{username:string}")]

Url will be:

www.example.com/someparam

Ha Doan
  • 611
  • 6
  • 20