Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a URL without the controller's function's name in ASP .NET MVC?

The default code that VS generates for HomeController is:

<HandleError()> 
Public Class HomeController Inherits System.Web.Mvc.Controller

    Function Index() As ActionResult
        ViewData("Message") = "Welcome to ASP.NET MVC!"

        Return View()
    End Function

    Function About() As ActionResult
        Return View()
    End Function
End Class

Let's say that I want to create a different URL for the about page without changing the method name. I've tried:

'
' GET: /Home/Aboutblah

But that doesn't work. When I go to http://localhost:1957/Aboutblah, I get a 404 from the ASP .NET server.

Consequently, I was wondering if the "GET " blob of text actually does anything and if it is possible to fiddle with URLs without diving into the ASAX file.

like image 871
Zian Choy Avatar asked Aug 03 '09 16:08

Zian Choy


3 Answers

Also, you can decorate the method with an attribute:

[ActionName("Aboutblah")]

James

EDIT I just noticed you're using VB. You'll have to translate into VB, maybe:

<ActionName("Aboutblah")>

?

Also, yes, the comments, ie:

' Get /Home/Index

are just that -- comments.

like image 93
James S Avatar answered Oct 01 '22 02:10

James S


You will need to modify the code that sets up the routes.

If it's just a one-off, you can set up a specific route for this url.

The following would explicitly map url "Home/aboutBlah" to action HomeController.About action and use the standard controller/action route url pattern for any other urls:

RouteTable.Routes.MapRoute("CustomAboutUrl", "Home/aboutBlah", new {controller = "Home", action = "About"});

RouteTable.Routes.MapRoute("Default", "{controller}/{action}");

like image 23
Dan Malcolm Avatar answered Oct 01 '22 04:10

Dan Malcolm


Look into URL Routing, you should be able to define alternative URLs for the controller actions just fine. :)

like image 23
Rytmis Avatar answered Oct 01 '22 02:10

Rytmis