2

Using .Net 4, isn't it possible to include forward slash in route parameters or one should force the user to use hyphen - instead

Example: mysite.com/user/{id}/{year} maps to mysite.com/user/01/2012

where {id} = {user/01} and {year} = 2012 in the example above

if this is the last route parameter it would be easier as pointed in the answers below. but it is not

I have read these, same questions as mine

but I didn't really get what I want. I could use this solutions if I want to generate the URL.

But what happens if the use type the url in the browser?

Or one should avoid that and force the user to use other formats like replacing "/" with "-"? as: mysite.com/user/usr-01/2012

4
  • 1
    So what is the actual question here? Try listing what you want vs what actually happens. Jun 23, 2012 at 14:51
  • I have by question in bold already. Should one force user to use hyphen or is still possible to allow them to use "/". Or is it possible to intercept the request before it is resolved into route parameters? Thanks
    – codingbiz
    Jun 23, 2012 at 14:55
  • 1
    I think it's still not exactly clear what you want to achieve? If a user types in an invalid URL, you can respond with a friendly 404 that explains that this is not a valid url, and contains a link to user search form. Or give us an example of the url you would like to use. Jun 23, 2012 at 15:11
  • the question is clear enough and there are examples included - I updated my question
    – codingbiz
    Jun 23, 2012 at 15:13

2 Answers 2

3

Loosely interpreted: Can a route-parameter include a forward-slash ?

Answer: No. Just avoid that.

4
  • You mean I should tell my users to replace "/" with " - "? It could be part of my documentation if that is the last solution.
    – codingbiz
    Jun 23, 2012 at 15:00
  • Why/where would your users type a '/' in the first place? Jun 23, 2012 at 15:04
  • their user id is like mhis/2012/001 I don't have control over that except if I tell them swap "/" for " - " every time
    – codingbiz
    Jun 23, 2012 at 15:10
  • 3
    Users shouldn't be typing (complicated) urls. Tools should create those URLs and use a proper encoding (- will do). Jun 23, 2012 at 15:12
1

If I got you right. The userID is "mhis/001", and the year is 2012, then you would like to use an URL like this: http://localhost:59025/User/mhis/001/2012

Global.asax - define the route:

routes.MapRoute(
                "UserDetails", // Route Name
                "User/{idpart1}/{idpart2}/{year}", // URL with parameters
                new { controller = "User", action = "Details", year= UrlParameter.Optional} // Parameter defaults
            );

The "year" parameter is set as optional, so if the user type http://localhost:59025/User/mhis/001/2012, year will be 2012, if doesn't type anything, year will be null.

A dummy user class:

public class User
{
    public String Id { get; set; }
    public String Name { get; set; }

    /// <summary>
    /// just a helper to get the first part of the userID
    /// </summary>
    public string IDPart1 { get { return Id.Split('/')[0]; } }
    /// <summary>
    /// just a helper to get the second part of the userID
    /// </summary>
    public string IDPart2 { get { return Id.Split('/')[1]; } }
}

Added two helper properties to the User class take the user name apart (IDPart1, IDPart2), so it's easier to use in the view

The controller with a dummy user list:

public class UserController : Controller
{
    /// <summary>
    /// user list...
    /// </summary>
    List<User> _users = new List<User> {
        new User{ Id="mhis/001", Name="John Smith"},
        new User{ Id="mhis/002", Name="Some Body Else"}
    };    

    /// <summary>
    /// This is actually a user list, but...
    /// </summary>
    /// <returns></returns>
    public ActionResult Index()
    {
        return View(_users);
    }

    public ActionResult Details(string idpart1, string idpart2, int? year)
    {
        //do what you have to do with the year...
        ViewBag.year = year;            

        string realUserID = String.Format(@"{0}/{1}", idpart1, idpart2);
        var user = _users.FirstOrDefault(u => u.Id == realUserID);

        return View(user);
    }
}

The list view:

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>            
                @Html.ActionLink("Details", "Details", new { idpart1 = item.IDPart1, idpart2 = item.IDPart2 }) 
            </td>
        </tr>
    }

The interesting part is this: @Html.ActionLink("Details", "Details", new { idpart1 = item.IDPart1, idpart2 = item.IDPart2 }), sou you get what you want.

Details view:

    @model MvcApplication1.Controllers.User

    @{
        ViewBag.Title = "Details";
    }
    @if(Model != null) {
    <h2>Details</h2>

    <fieldset>
        <legend>User</legend>

        The year is: "@ViewBag.year"

        <div class="display-label">Name</div>
        <div class="display-field">
            @Html.DisplayFor(model => model.Name)
        </div>
    </fieldset>

    <p>@Html.ActionLink("Back to List", "Index")</p>

    } else {//this should be handled in the controller...
        <h2>User not found!</h2> 
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.