Manoz
Manoz

Reputation: 6617

adding custom view name in asp.net mvc4

I am building an application in .net mvc4 that is based on business promotion and sales store.

User on this web application would be able to use product and access his/her personal business page also, that page can be promoted in future.

So I added one controller- Mypanel and a view of user's personal or professional business page _Mypanel.

Now the url access to this page is Bizcopter.com/Mypanel/_Mypanel

I want a custom user defined page name-

i.e. If a business name is - BookStore Then I want to add a view in this same controller with the name of BookStore, So URL of personal business page would be-

Bizcopter.com/Mypanel/BookStore/ and this business holder can promote his business page with this URL.

Let me know if these are possible-

  1. Replacing the view's name of user's choice
  2. Add a view from client side in this same controller

I don't have any idea how to make it happen so don't have any trying code.

Site URL- http://bizcopter.com/Mypanel/_Mypanel

Upvotes: 0

Views: 736

Answers (3)

Selman Genç
Selman Genç

Reputation: 101732

As the others said you can add a new route.Consider this code:

routes.MapRoute(
            name: "MyCustomRoute",
            url: "MyPanel/{name}"
            defaults: new  { controller = "MyPanel", action = "MyAction", name="" }
        );

In this route if user type this URL:

Bizcopter.com/Mypanel/

Then it goes to your MyAction in your MyPanel Controller by default.Actually it will always go to MyAction, and in your MyAction, you must take the name parameter and redirect to user to the Relevant Action like this:

public ActionResult MyAction() 
{
    var name = RouteDate.Values["name"];

    // check the name and redirect user to another action if it necessary

    if(name == "BookStore") return RedirectToAction("BookStore","Mypanel");
}

Upvotes: 1

Jon Douglas
Jon Douglas

Reputation: 13176

By default you'll have a route in your AppStart/RouteConfig.cs which may look like this:

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

The default URL structure might look something similar to: http://localhost:{PortNumber}/{Controller}/{Action}

Where we can attribute the following:

Controller = Home

Action = Index

Now if you want something similar to how you have it, you'd want something along the lines of:

http://testing.com/Fruits/Apples

Controller = Fruits

Action = Apples

By default, a URL pattern will match any URL that has the correct number of segments, in this case {controller}/{action}

Overall you should just need the MyPanel controller, and a controller taking a parameter of string which loads the correct Object/Model into the view.

Source: Pro ASP.NET MVC 4 - Adam Freeman

Upvotes: 2

Joe Ratzer
Joe Ratzer

Reputation: 18569

You don't need a separate view and controller action for each business.

I would create a controller and view called MyPanel. The controller takes a parameter called something like businessName that will load data related to the parameter.

Upvotes: 2

Related Questions