Vlince
Vlince

Reputation: 6011

Do not display Action in URL - Routing

I have a DirectoryController with an Index() ViewResult which displays the following links in the view:

@Html.ActionLink("My file 1", "Index", "File", new { @id = "123" }, null) <br />
@Html.ActionLink("My file 2", "Index", "File", new { @id = "456" }, null) <br />
@Html.ActionLink("My file 3", "Index", "File", new { @id = "789" }, null) <br />

If I click on one of the links, I’ll end up having (for example) the following in the URL:

http://www.domain.com/File/Index/123

What I’d really like to have is the following in the URL:

http://www.domain.com/File/my-file-1

Basically, I’d like to see the Controller name and a custom name (the name of the file for example) in the URL without showing the actual action or the id.

Regardless of the link I click, all of them should continue to point to the Index() method of the FileController passing along the appropriate id.

How would I go about this?

How and what kind of custom route should I create?

Thanks

Upvotes: 0

Views: 1066

Answers (2)

user338195
user338195

Reputation:

In case you'll be running this on IIS, rather than Casini, you can use URL re-writes to achieve the desired result.

Upvotes: 0

cpoDesign
cpoDesign

Reputation: 9153

As general overview:

what you want to do is create route mapping

eg translating url that is user friendly

you would need to do some updates in your controller/ global.asax

example of route

  context.MapRoute(
              "FileNameMapping",
              "File/{filename}",
              new {controller="File", action = "Index" });

in your action you will have something like

Public FileResult Index(string filename){
// do your logic for handleling file name 
//and return file
}

Upvotes: 1

Related Questions