How to change the names of the url action - asp.net-mvc-3

In my url instead of localhost:45120/Home/Index we need website names like stackoverflow like that.
How can i implement that for our project using asp.net mvc3.

Then , you should register a domain name from any domain hosting company. you will have to buy web space from hosting company and redirect your domain name to the ip of the web space you have purchased...
**EDIT:**
routes.MapRoute(
"Can Be anything", // Route name
"yourpath/{para1}", // URL with parameters[if you want to have any]
new { controller = "Quote", action = "Index" }, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);

Related

web api support parameters while they don't have one

http://localhost:xxxx/api/BindAppointmentResources
works fine for me but when I'm trying to add any invalid object after controller (with this ? "http://localhost:xxxxx/api/BindAppointmentResources?Userid") in URL its gets the same result
I tried action-based routing , attribute routing so far but same result?
PS : I don't have parameters in WEB API
Route Config :
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Query string parameters will automatically get bound to your parameters that have the same name in web api.
If no parameter names are found it will route the request to the base url (which is the url before the question mark).
So this url
http://localhost:xxxx/api/BindAppointmentResources?UserID=1
will, if no method with parameter name that match UserID are found, end up being routed to
http://localhost:xxxx/api/BindAppointmentResources
In the Get method you can still get the query string
var queryString = this.Request.GetQueryNameValuePairs();
To prevent binding parameters from query string and only allow bindings from route values, you could remove the default QueryStringValueProviderFactory from the default HttpConfiguration configuration. Read more about it in this article

In MVC 6, URL rewriting

I have one authentication site, and it receives requests from multiple sites(site 1,2,3) for authenticate the user.
exp:
1. site/site 1/controller/action
2. site/site 2/controller/action
3. site/site 3/controller/action
I would like to display above URLs as
site/custom text/controller/action
I need to replace the site 1, site 2,site 3 as custom text
and perform the actions ...
Please help on this.....
Regards
P
Yes can can do it by some simple steps
1.)Add the custom MapRoute in Startup.cs file in app.UseMvc section like this :-
app.UseMvc(routes =>
{
routes.MapRoute(
name: "team",
template: "Site/{DiffrentSiteName}/{controller}/{action}/{id?}",
defaults: new { controller = "DefaultcontrollerName ", action = "Index" }
});
2.)Then in the default control index method get that site name like this
public IActionResult Index(string DiffrentSiteName)
{
var siteName = DiffrentSiteName;
return view();
}
3.)We have to use the same name to get the value of site from the url
in controller like we have used above (DiffrentSiteName in url and in
index method)
4.) So the url will be like :
1.)http:Site/Site1name/controller/action/parm
2.)http:Site/Site2name/controller/action/parm
3.)http:Site/Site3name/controller/action/parm
5.)So when you get the name of the Site in the index
method you can respond accordingly or according to the
site name

Hiding the Default form authentication url in asp.net mvc

I have an application which is configured to use
~/Account/LogOn
in the
web.config
file for the authentication.
I would like to have the URL just point to www.example.com instead of www.example.com/Account/LogOn.
I have tried to have the routing configuration as follows, but it does not work
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = "" }
);
Kindly suggest the right practice that can be used. I tried to remove the loginurl from web.config file but it is not of use and shows authorization error while running.
I'm not sure you may change routing to have the same address for two actions: Home/Index and Account/LogOn. But if you want change default logOn routing you need 2 steps:
1) Add one more routing:
//This route returns www.example.com/Login
routes.MapRoute(
"MyRoute",
"Login",
new { controller = "Account", action = "LogOn", id = "" }
);
2) Make changes in web.config:
~/Login
In the same way you may create any other routing for LogOn
As for me the only solution to have login on Index page is to do like this (and delete redirect from web.config):
#if(!Request.IsAuthenticated)
{
//PartialView with Log In form
}
else
{
// Your Index page content
}

MVC3 Url Routing to create member area

I would like to create a member area on my site with the following URL patterns:
Pattern for logged out user:
domain.com
domain.com/About
domain.com/Blog
domain.com/Blog/1 (where 1 is the post ID)
But I also have a member area where I prefix the Url with Member like this:
domain.com/Member/MyProfile
domain.com/Member/MySettings
This seems simple, but I can't see an obvious way to make routing rules for this. I have:
routes.MapRoute(
"Member", // Route name
"Member/{controller}/{action}/{id}", // URL with parameters
new { controller = "Task", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This works great for the member when you are logged in, but unfortunately the first rule also matches the logged out view and Url.Action("Blog", "Home") produces a Url that looks like this:
domain.com/Member/Home/Blog
How do I tell Url.Action that it must form Urls with the default rule outside the member area?
You could use a real MVC area instead of trying to simulate one. There's also a video you might checkout. The idea is that you leave your default route definition in Global.asax and then add a Member area to your site which will have a separate route configuration.

ASP.NET Controller name conflicted with a folder name

In my ASP.NET MVC3 project, I have a folder called Content (the default folder for an MVC project). But I also have a controller called Content. And when I want to use the default actions of this controller, I simply use http://domain/content/, which is equivalent to http://domain/content/index. But IIS returns 403 error and thinks that I'm gonna get the directory list of the Content Folder. Well, this question is already discussed in this question. But I don't know how to rewrite my URL to append the default action to it. May someone help please.
You can get around this by changing routing configuration to specify:
routes.RouteExistingFiles = true;
You will then need to set up some ignore rules to prevent genuine static content being gobbled up by the routing engine.
For example, I have a folder called Touch in my app, and I also have a specific route for Touch. So the working config is:
routes.RouteExistingFiles = true;
routes.IgnoreRoute("Touch/Client/{*touchclientversion}", new { touchclientversion = #"(\d*)(/*)" });
I agree that this kind of thing should generally be avoided, but sometimes it's nice to have pretty URLs :-)
You can add a default route like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
In your case:
routes.MapRoute(
"DefaultContent", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Content", action = "Index", id = "" } // Parameter defaults
);

Resources