ASP.NET MVC Base Address changes when publishing to IIS 7 - asp.net-mvc

I published an MVC4 app on IIS and my base adrress changed to localhost/somevirtualdirectory/Home/Index.
That works fine however when I click on a ActionLink ( tag) I'm being redirected to localhost/someController/Action and that fails since the somevirtualdirectory is missing in the url.
How can I set the the action will redirect to the baseadrress with the virtual directory?

This happens because in your action link you have entered your URL hard coded starting with slash which will throw you to the parent directory and thus excluding the subdirectory. Use Url.action to form the URL which will include your sub-directory when you hit :
#Url.Action("Action_Name", "Controller_Name")
there is a difference. Html.ActionLink generates an tag whereas Url.Action returns only an url.
For example:
#Html.ActionLink("link text", "someaction", "somecontroller", new { id = "123" }, null)
generates:
link text
notice this slash at the beginning of the href which will take you to the root .
and Url.Action("someaction", "somecontroller", new { id = "123" }) generates:
/somecontroller/someaction/123
Now when you click It will append it on the sub directory

Related

Get url for custom route in asp.net mvc

In my ASP.Net mvc application I have changed the default route registration in startup.cs to
routes.MapRoute(
name: "default",
template: "{customer}/{language=fi-FI}/{controller=Home}/{action=Index}/{id?}");
So all routes would contain the customer id and the current language. Custom segment matching takes care of putting the customer and language info in new urls so when the requested url is /customer1/fi-fi/somecontroller/someaction any generated url like this
<a asp-controller="othercontroller" asp-action="otheraction">Some action</a>
would be generated correctly with the customer and language code in it.
The question is, how should I generate the url so I can specify the customer and language code without having to do string concatenation? I would need this e.g. in the links for changing language.
I have tried
<a asp-controller="somecontroller" asp-action="someaction" asp-route="default" asp-route-customer="customer2" asp-route-language="en-us">Some action in other language and other customer</a>
but that says that I cannot specify action and controller when asp-route is defined and if I remote asp-route then nothing changes.
I got this working by using #Url.RouteUrl, e.g.
#Url.RouteUrl("default", new { customer = "someothercustomer", language = "someotherlanguage", controller = "somecontroller", action = "someaction" })

I want to open web form within MVC 5 project

I am using MVC5 with attribute routing. Below is the code for default route
RouteTable.Routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new
{
controller = "Home",
action = "Index"
},
namespaces: new[] { "Web.Controllers" }).DataTokens["UseNamespaceFallback"] = false;
Now I have a requirement to open a .aspx page from this project to render some SSRS reports.
So I add a new folder "Report" and put a .aspx page inside this.
To open this page I just write the url on onclick function of button without using any controller.
To add route for this I used the below route in my Route.config file just above the default route.
RouteTable.Routes.MapPageRoute("Report", "Reports/{ref}", "~/Reports/CustomerQuotation.aspx");
Now the problem is that I am able to open this page from my machine but when I publish the code and deploy this site on dev server and access this url.
This page ask me for authentication, means username and password and If type anything then it shows me 404 request not found.
Please help me on this. I just need to open an aspx page from mvc5 application.

Changing URL without changing Actual Path to Redirect

I am new to ASP.Net and working on MVC 4. I want to replace my current URL with a customized URL.
For example:
Current URL: http://www.testsite.com/home?pageId=1002
Desired URL: http://www.testsite.com/1002/home/
So the URL that is displayed in the address bar will be the desired one and actual URL working will be the current one.
I have tried URL routing in Global.asax file of my project but doesn't seems to be working for me.
What exactly I want is to put the URL Like this.
Thanks in Advance.
ASP.NET MVC 4 provide a toolbox way to write your application. The URL that you see in the browser comes from Routing that do the hard work to convert url to app routes and app routes to url.
1) The default ASP.NET MVC 4 Template project comes with a file at App_Start folder named RouteConfig, where you must config the routes for the app.
2) The routes has precedence order, so, put this route before the default one:
routes.MapRoute(
name: "RouteForPageId",
url: "{pageId}/{action}",
//controller = "Home" and action = "Index" are the default value,
//change for the Controller and action that you have
//pageId is the parameter from the action that will return the page
defaults: new { controller = "Home", action = "Index" }
);
Now you can enter myappdomain/1220/index for exemple.
Hopes this help you! Take a look here for more info ASP.NET Routing!

Deployed mvc app not including sitename in href links

I have an asp.net MVC web app using Kendo UI Grids with client templates. The client template specifies an href as follows:
.ClientTemplate("<a href='[controller]/[action]/[parameters]' />")
In a debug run, the link is correctly generated as follows:
http://localhost:[port]/[Controller]/[Action]?[parameters]
works beautifully.
However, in a deployed environment, the link generated is as follows:
http://[server]/[Controller]/[Action]?[parameters]
And that fails because it is missing the website name. So what I need is for it to generate the links as follows:
http://[server]/[WebSite]/[Controller]/[Action]?[parameters]
How do I need to specify my href links for this to work?
Don't hand-code your url's instead of use the UrlHelper which will take care of including the virtual directory into your urls:
.ClientTemplates(string.Format("<a href='{0}' />",
Url.Action("action", "contoller", new { param1, param2})))
If you want to use the client template value in the url you have to build the url dynamically because the Kendo UI template syntax won't inside the route value:
columns.Bound(m => m.Id)
.ClientTemplate(string.Format("<a href='{0}&Id=#= Id #'>Click me<a>",
Url.Action("action", "contoller", new { someNotTemplatedParam })));

Running ASP.NET MVC in a subdomain makes Html.ActionLink render broken links

I am running MVC in a subdomain
http://test.domain.com which points to the /Test directory on my webhost4life account.
Html.ActionLink("About", "About", "Home")
it renders a link to
http://test.domain.com/Test/Home/About -- which gives a 404
the link should be ..
http://test.domain.com/Home/About
is there a way to override ActionLink to omit the /Test on render?
Thank you
Experiment 1
I added a route to the table like this...
routes.MapRoute(
"Test", // Route name
"Test/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
and now action link renders links like this..
http://test.domain.com/Test/Test/Home/About/
when this is clicked it does not give a 404 but gives the Home controler About action.
Result
No more broken links but the site renders ugly urls.
For a site using lots of subdomains I use a nifty MVC extension from ITCloud called UrlRouteAttribute. It allows you to assign a route to every action as an attribute setting the path and name. I have extended this to allow fully qualified paths - so to include the domain/subdomain the controller should attach to. If this is something you'd be interested in I'll upload a copy somewhere.

Resources