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.

7
  • 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. Commented Jul 9, 2016 at 7:06
  • @StephenMuecke thanks for reply, i understand the performance issue.Should i include atleast action ? Commented Jul 9, 2016 at 7: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) Commented Jul 9, 2016 at 7: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 Commented Jul 9, 2016 at 7: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. Commented Jul 9, 2016 at 7:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i already implemented that, thanks for help, +1
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

1 Comment

Thanks for help, but it implies on api, +1 for effort

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.