1

The issue arises with a map route I have added to my RouteConfig.cs file, which maps routes to my Topic controller.

Here's my RegisterRoutes method, from global.asax:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "ArticleByTitle",
    url: "{controller}/{action}/{title}/{category}",
    defaults: new { controller = "Topic", action = "Get", category = UrlParameter.Optional }
);

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

The Topic controller contains the specified action method:

namespace Wiki.Controllers
{
    public class TopicController : Controller
    {
        public ActionResult Get(string title, string category)
        {
            var topics = this.GetTopicsList();
            var count = topics.Count(t => t.Category.Equals(category, StringComparison.CurrentCultureIgnoreCase)
                                          && t.Title.Equals(title, StringComparison.CurrentCultureIgnoreCase));
            if (count > 1)
            {
                return RedirectToAction("Resolve", new { title = title });
            }

            if (count == 0)
            {
                return new HttpNotFoundResult();
            }

            var selectedTopic = this.GetTopicsList().FirstOrDefault(t => t.Title.Equals(title, StringComparison.CurrentCultureIgnoreCase));
            return RedirectToAction("Display", new { topic = selectedTopic });
        }

    }
}

However, this method is never invoked. Instead, I am receiving an HTTP 404 error when I attempt to reach it through a link in the application that looks like this:

<a href="/topic/display/MyTopic">MyTopic</a>

I am assuming that there is something wrong with my routing configuration, but for the life of me, I can't see what it is. Could someone kindly point me in the right direction?

2
  • In your href you're missing the action (which in your case is 'Get') Commented Jul 12, 2014 at 14:37
  • @Dimitar, Can you make this an actual answer? This is the solution I was looking for. It's always something simple, stupid, and obvious. :) Commented Jul 12, 2014 at 14:45

3 Answers 3

1

It seems that in your href you're missing your action (which in your case is Get)

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

Comments

1

For the Get action to be invoked your URL would need to look like this:

<a href="/topic/get/mytitle/mycategory"></a>

You should also generate the link by using the Html ActionLink helper like this:

@Html.ActionLink("MyTopic", "Get", "Topic", new { title = "sometitle"}, null)

5 Comments

The category is an optional parameter.
<a href="/topic/get/mytitle"></a> would also work. What I'm saying is your orginal URL is mapped to the display action, which means it will never call the Get action unless you have some sort of redirect in the Display action that you have not shown in your code.
Further, the action won't even enter the picture if the controller is never invoked.
If I set a breakpoint in the first line of the Get action, it's never hit.
The first parameter in the URL maps to the controller name, the second parameter maps to the action. Your original URL is mapping to the Topic controller Display action. /topic/display.
0

Try using the following syntax

action="@Url.Action("Go", "Home")" 

for your HREF link.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.