This project is read-only.

URL routing issue

Aug 25, 2013 at 7:29 PM
Edited Aug 25, 2013 at 7:30 PM
Dear Friends,

I am facing a problem while creating a new URL which will fetch topic details based on forum category instead of just forum.

For this I have added following route to Routes.config:
 <add url="{forumCategory}/{page}" controller="Forums" action="CategoryDetails"/>
Here is action link:
@Html.ActionLink(category.Name, "CategoryDetails", "Forums", new { forumCategory = category.Name, page = 0 }, new { })
where category is object of ForumCategory class.

This action link should point to following action method in ForumsController:
public ActionResult CategoryDetails(string forumCategory, int? page)
        {
...
}
But it's pointing to following action method in ForumsController:
public ActionResult Detail(string category, int? page)
        {
..
}
Can somebody tell me where am I going wrong ? How can I make it point to CategoryDetails action method.
Thanks a lot for your kind help.

Regards
Suresh
Aug 26, 2013 at 1:56 AM
Where did you add it in the list?

You will to add it before the following config:
    <add url="{forum}/{page}" controller="Forums" action="Detail">
        <constraints>
            <add key="forum" value="[\w\-%]+" />
            <add key="page" value="^\d+$" />
        </constraints>
        <defaults>
            <add key="page" value="0" />
        </defaults>
    </add>
As well, you will need to adjust one, as they both are same signature, either by adding constraints for category to limit by your specified categories or changing url structure.
Aug 27, 2013 at 1:28 PM
Edited Aug 27, 2013 at 1:29 PM
Hi Azsael,

Thanks for you response.

I put it at the end of Routes.config file.
If I put this route above Forums > Detail route. then I guess Forums > Detail route will not work as it will point to Forums > CategoryDetails.
Also I need to handle to different data fetch operations so adjusting to one will be lil difficult.

As per existing login Forums > Details is retrieving all topics under that forum while I need to have one more action link which will fetch all topics from ForumsCategory.

If ForumsCategory is MVC and mvc-forum1 and mvc-forum2 are two forums under MVC ForumsCategory. I need to generate a action link (MVC) which will fetch all topics from all forums under that ForumsCategory (here from mvc-forum1 and mvc-forum2)

Which I am trying to achieve by:
@Html.ActionLink(category.Name, "CategoryDetails", "Forums", new { forumCategory = category.Name, page = 0 }, new { })
with route:
<add url="{forumCategory}/{page}" controller="Forums" action="CategoryDetails"/>
But still it pointing to Forums > Details (as you said, this is due to same signature which I agree).

Can you please provide some other solution ? Or any ideas ?
Thank you once again for your kind help.

Thanks & Regards
Suresh.
Aug 28, 2013 at 2:44 AM
There's two approaches you could take, if you are very strict on the url structure you can do the following:

<add url="{forumCategory}/{page}" controller="Forums" action="CategoryDetails">
    <constraints>
        <add key="forumCategory" value="(Category1|Category2|Category3)" />
    </constraints>
    <defaults>
        <add key="page" value="0" />
    </defaults>
</add>
Where (Category1, Category2, Category3 are the names of your categories)
This is weak, however it needs to map all your to categories which are data related, and not have the same name as a forum.

i.e. If you have a Category with same name as forum this won't work.

Best way would be to add a "static" section to the url. i.e. change either one.

<add url="category/{forumCategory}/{page}" controller="Forums" action="CategoryDetails"/>
Sep 18, 2013 at 2:02 PM
Hi Azsael,

Thanks a lot for your suggestion. I tried your first approach which almost solved my issue but not completely.
With this, forums with unique name works perfect, but in case if I have forum name: forum-one and category as: one it doesn't work.

Let me explain. I have defied route as per your first approach with constraints just above forum details route in routes.config file.

Now I have route as:
<add url="{forumCategory}/{page}" controller="Forums" action="CategoryDetails">
<constraints>
        <add key="forumCategory" value="(one|two|three)" />
    </constraints>
    <defaults>
        <add key="page" value="0" />
    </defaults>
</add>
Now if I try
http://samplesite.com/forum-one/
It should point to forums detail action method, but instead it points to categorydetails action method. If I try unique forum name which is distinct from forumCategories defined in above constraints like:
http://samplesite.com/forum-unique/
This works perfect. Can you please help me how can I resolve this issue.

Thanks a lot for your kind help.
Sep 21, 2013 at 8:11 AM
Hey sureshdrim,

As I specified before, the Forum categories and Forum names will need to be unique for this to work, but updating the regex constraint part to: ^(one|two|three)$ should fix the issue you are having with partial matches (So it matches to start and end of string). I haven't completely tested this, so you may need to adjust (http://regexpal.com/) is pretty good if you need help.


This won't fix if you have Forum: "one" and Category: "one" though, FYI.