1

What I want to do : is to add prefix after action name, so that the browser's url bar can be seen like.

http://stackoverflow.com/ask
                 ↓↓↓↓
http://stackoverflow.com/ask.mon

And also want to invoke a specific controller with the prefix as well.

http://stackoverflow.com/loginProcess.mon

If invoking like above, then below action can be called.


UPDATED : ( remember, I don't want to show my controller's name on the url bar. )


public Class LoginController : Controller
{
     public ActionResult loginProcess() {
          return View();
     }
}
.
.

What I did : set up route config like this below.

   public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

and the controller looks like...

 public class LoginController : Controller
    {        
        public ActionResult Index()
        {
            if (Session["userInfo"] != null)
            {
                return RedirectToAction("Main", "Web");
            }
            else
            {
                return View();
            }
        }
    .
    .
    .

What happened : Throws me 404 error.

What should I do to avoid this?

hina10531
  • 3,938
  • 4
  • 40
  • 59

2 Answers2

0

To get your requested url (http://stackoverflow.com/login/loginProcess.mon) to work, your mapping does not include the controller. The following code should fix the routing.

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}.mon/{id}", // Added {controller} to routing so Login/loginProcess.mon will work
    defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);

Then your code for the action in the LoginController should work great.

public ActionResult loginProcess() {
    return View();
}
Jason W
  • 13,026
  • 3
  • 31
  • 62
  • Sorry sir, I updated my question. I don't want controller's name to appear on the URL bar. – hina10531 Nov 13 '14 at 07:31
  • I am confused then by your two controllers by the same name in the question, one with action `index` and the second with action `loginProcess`. If you prefer the Index action to do the work, then replace {action} with the hard coded loginProcess.mon/{id}, and then leave the default action to index. – Jason W Nov 13 '14 at 10:56
0

Finally, I've found a solution, all you have to do is to add a module for routing.

This module gets your routing to locate the expected action.

<modules>
   <remove name="UrlRoutingModule-4.0" />
   <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
</modules>

And here's another option for this issue, although it's not a direct approach to this problem and this is probably an oversaturated method. But indirectly, it can solve this 404 error when a prefix comes after the action names. Just put this on your Web.config, <system.webServer> section

<modules runAllManagedModulesForAllRequests="true" />

This also has the same effect because it includes the module above as well.

I've picked up a few related information that might help you get a grasp of the feature.

Well, apparently, runAllManagedModulesForallRequests is not recommended by the way.

Community
  • 1
  • 1
hina10531
  • 3,938
  • 4
  • 40
  • 59